-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
58 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
// TypeScript defaults error type to unknown, this file is to handle error messages and types | ||
|
||
type ErrorWithMessage = { | ||
message: string | ||
} | ||
message: string; | ||
}; | ||
|
||
function isErrorWithMessage(error: unknown): error is ErrorWithMessage { | ||
return ( | ||
typeof error === 'object' && | ||
error !== null && | ||
'message' in error && | ||
typeof (error as Record<string, unknown>).message === 'string' | ||
) | ||
return ( | ||
typeof error === "object" && | ||
error !== null && | ||
"message" in error && | ||
typeof (error as Record<string, unknown>).message === "string" | ||
); | ||
} | ||
|
||
function toErrorWithMessage(candidate: unknown): ErrorWithMessage { | ||
if (isErrorWithMessage(candidate)) { | ||
return candidate | ||
} | ||
try { | ||
return new Error(JSON.stringify(candidate)) | ||
} catch { | ||
return new Error(String(candidate)) | ||
} | ||
if (isErrorWithMessage(candidate)) { | ||
return candidate; | ||
} | ||
try { | ||
return new Error(JSON.stringify(candidate)); | ||
} catch { | ||
return new Error(String(candidate)); | ||
} | ||
} | ||
export function getErrorMessage(error: unknown) { | ||
return toErrorWithMessage(error).message | ||
return toErrorWithMessage(error).message; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters