Created
February 28, 2023 15:17
-
-
Save akellbl4/35dbd9b3e025e68322808b5906460c85 to your computer and use it in GitHub Desktop.
Custom error handling
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
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