Created
August 28, 2023 17:25
-
-
Save nathansmith/41aef197086c62ef1fdb5ae6fefa5b33 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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