Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions frontend/src/app/common/util/error.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { extractErrorMessage } from "./error";

describe("extractErrorMessage", () => {
it("should return the message from an Error instance", () => {
const testError = new Error("error instance");
const result = extractErrorMessage(testError);
expect(result).toBe(testError.message);
});

it("should return the error string when error is a plain string", () => {
const testError = { error: "boom" };
const result = extractErrorMessage(testError);
expect(result).toBe(testError.error);
});

it("should return the nested message when error is an object with a message", () => {
const testError = { error: { message: "nested" } };
const result = extractErrorMessage(testError);
expect(result).toBe(testError.error.message);
});
Comment on lines +35 to +39

it("should return the fallback message for when error is null", () => {
const testError = null;
const result = extractErrorMessage(testError);
expect(result).toBe("An unknown error occurred.");
});

it("should return the fallback message when error is a number", () => {
const testError = 123;
const result = extractErrorMessage(testError);
expect(result).toBe("An unknown error occurred.");
});

it("should return the fallback message when error is an empty object", () => {
const testError = {};
const result = extractErrorMessage(testError);
expect(result).toBe("An unknown error occurred.");
});

it("should return the fallback message when error key value is neither a string nor an object with a message", () => {
const testError = { error: 123 };
const result = extractErrorMessage(testError);
expect(result).toBe("An unknown error occurred.");
});
});
Loading