Skip to content

Instantly share code, notes, and snippets.

@nathansmith
Created August 28, 2023 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathansmith/41aef197086c62ef1fdb5ae6fefa5b33 to your computer and use it in GitHub Desktop.
Save nathansmith/41aef197086c62ef1fdb5ae6fefa5b33 to your computer and use it in GitHub Desktop.
// Helper function to determine error message.
const getErrorMessage = (error: Error | ErrorEvent | PromiseRejectionEvent): string => {
// Set later.
let message = '';
// Message string?
if (typeof (error as Error)?.message === 'string') {
message = (error as Error)?.message;
// Reason string?
} else if (typeof (error as PromiseRejectionEvent)?.reason === 'string') {
message = (error as PromiseRejectionEvent)?.reason;
// Reason message string?
} else if (typeof (error as PromiseRejectionEvent)?.reason?.message === 'string') {
message = (error as PromiseRejectionEvent)?.reason?.message;
}
// Expose string.
return message;
};
// Export.
export { getErrorMessage };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment