Skip to content

Commit

Permalink
feat(utils): enhance getErrorMessage to handle custom error objects
Browse files Browse the repository at this point in the history
  • Loading branch information
kirklin committed Oct 16, 2023
1 parent c2cc5e9 commit b731015
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/web/utils/src/__test__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("getErrorMessage", () => {

test("should convert the error object to string if it is not an instance of Error", () => {
const error = { message: "This is not an Error instance" };
expect(getErrorMessage(error)).toBe("[object Object]");
expect(getErrorMessage(error)).toBe("This is not an Error instance");
});

test("should return the error message as string if the error object is a string", () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/web/utils/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,11 @@ export function memo<TDeps extends readonly any[], TResult>(
* @param error The error object to extract error message from. 用于提取错误信息的错误对象。
* @returns The error message string. 错误信息字符串。
*/
export function getErrorMessage(error): string {
export function getErrorMessage(error: Error | { message: string } | string): string {
if (error instanceof Error) {
return error.message;
} else if (isObject(error) && "message" in error) {
return error.message;
} else {
return String(error);
}
Expand Down

2 comments on commit b731015

@vercel
Copy link

@vercel vercel bot commented on b731015 Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

celeris-web-api – ./services/admin

celeris-web-api-kirklin.vercel.app
celeris-web-api-git-master-kirklin.vercel.app
celeris-web-api.vercel.app

@vercel
Copy link

@vercel vercel bot commented on b731015 Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

celeris-web – ./apps/admin

celeris-web.vercel.app
celeris-web-git-master-kirklin.vercel.app
celeris-web-kirklin.vercel.app

Please sign in to comment.