Skip to content

Instantly share code, notes, and snippets.

@akellbl4
Created February 28, 2023 15:17
Show Gist options
  • Save akellbl4/35dbd9b3e025e68322808b5906460c85 to your computer and use it in GitHub Desktop.
Save akellbl4/35dbd9b3e025e68322808b5906460c85 to your computer and use it in GitHub Desktop.
Custom error handling
class MyCustomError extends Error {
customField?: string
constructor(msg: string, customData: Record<string, string>) {
super(msg)
this.customField = customData.customField
}
}
try {
throw new MyCustomError('Custom Error', { customField: 'additional data' })
} catch (e) {
if (e instanceof MyCustomError) {
console.error(e.customField)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment