Skip to content

Instantly share code, notes, and snippets.

@SpenceDiNicolantonio
Last active May 3, 2024 02:40
Show Gist options
  • Save SpenceDiNicolantonio/c7a8ecff4e38b9a921eedb56a2e8bfcf to your computer and use it in GitHub Desktop.
Save SpenceDiNicolantonio/c7a8ecff4e38b9a921eedb56a2e8bfcf to your computer and use it in GitHub Desktop.
Typescript Error class #typescript
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