Last active
May 3, 2024 02:40
-
-
Save SpenceDiNicolantonio/c7a8ecff4e38b9a921eedb56a2e8bfcf to your computer and use it in GitHub Desktop.
Typescript Error class #typescript
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
type ErrorName = 'Unknown Error' | 'Initialization Error'; | |
export class ProjectError extends Error { | |
name: ErrorName; | |
data?: Record<string, unknown>; | |
constructor({ name, message, data }: { name: ErrorName; message: string; data?: Record<string, unknown> }) { | |
super(); | |
this.name = name; | |
this.message = message; | |
this.data = data; | |
} | |
} | |
export function wrapError(error: Error | string, name: ErrorName = 'Unknown Error'): Error { | |
return new ProjectError({ | |
name, | |
message: error instanceof Error ? error.message : error, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment