-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add better error typings (#103)
* feat: add better error typings * fix: update package.lock * feat: refactor * feat: update version * fix: add try/catch and resolve * fix: error schema * fix: remove useless try/catch * fix: add endline after package lock * fix: add endline after package lock * feat: rename error methods * feat: update version of package
- Loading branch information
Showing
6 changed files
with
125 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ErrorEvent, ErrorEventSchema } from '../validators/events' | ||
|
||
export enum ErrorCode { | ||
'SIGNED_REQUEST_EVENT' = 'SignedRequestEvent', | ||
'SIGNED_REQUEST_JWT' = 'SignedRequestJWT', | ||
'RESPONSE_CALLBACK_EVENT' = 'ResponseCallbackEvent', | ||
'VERIFIABLE_PRESENTATION_SCHEMA' = 'VerifiablePresentationSchema', | ||
'ERROR_EVENT' = 'ErrorEvent', | ||
} | ||
|
||
function getIssue(errorEvent: ErrorEvent) { | ||
return errorEvent.error.details![0].issues && | ||
errorEvent.error.details![0].issues.length > 0 | ||
? errorEvent.error.details![0].issues | ||
: errorEvent.error.message | ||
} | ||
|
||
export function getUnexpectedErrorMessage(code: ErrorCode) { | ||
return `Unexpected error occured. Error Code: ${code} ` | ||
} | ||
|
||
export function formatEventError(errorEvent: ErrorEvent): string { | ||
return `Something went wrong. ${getIssue(errorEvent)}. Error Code ${errorEvent.error.httpStatusCode}` | ||
} | ||
|
||
export function throwEventParsingError(event: ErrorEvent): never { | ||
let errorEvent: ErrorEvent | ||
try { | ||
errorEvent = ErrorEventSchema.parse(event) | ||
} catch (e) { | ||
throw Error(getUnexpectedErrorMessage(ErrorCode.ERROR_EVENT)) | ||
} | ||
throw Error(formatEventError(errorEvent)) | ||
} |
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