-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce new error classes: - BaseError : Base class for all other errors - UnexpectedError : Error that was caused a programmer error - OperationalError : A transient error like timeout - UserError : Error that was caused by user's action or input
- Loading branch information
Showing
11 changed files
with
184 additions
and
21 deletions.
There are no files selected for viewing
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,50 @@ | ||
import type { Event } from '@sentry/node'; | ||
import callsites from 'callsites'; | ||
|
||
import type { ErrorTags, ErrorLevel, ReportingOptions } from '@/errors/error.types'; | ||
|
||
export type BaseErrorOptions = { description?: undefined | null } & ErrorOptions & ReportingOptions; | ||
|
||
/** | ||
* Base class for all errors | ||
*/ | ||
export abstract class BaseError extends Error { | ||
readonly level: ErrorLevel; | ||
|
||
readonly tags: ErrorTags; | ||
|
||
readonly shouldReport: boolean; | ||
|
||
readonly description: string | null | undefined; | ||
|
||
readonly extra?: Event['extra']; | ||
|
||
readonly packageName?: string; | ||
|
||
constructor( | ||
message: string, | ||
{ | ||
level = 'error', | ||
description, | ||
shouldReport = true, | ||
tags = {}, | ||
extra, | ||
...rest | ||
}: BaseErrorOptions = {}, | ||
) { | ||
super(message, rest); | ||
|
||
this.level = level; | ||
this.shouldReport = shouldReport; | ||
this.description = description; | ||
this.tags = tags; | ||
this.extra = extra; | ||
|
||
try { | ||
const filePath = callsites()[2].getFileName() ?? ''; | ||
const match = /packages\/([^\/]+)\//.exec(filePath)?.[1]; | ||
|
||
if (match) this.tags.packageName = match; | ||
} catch {} | ||
} | ||
} |
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,23 @@ | ||
import type { BaseErrorOptions } from '@/errors/base/base.error'; | ||
import { BaseError } from '@/errors/base/base.error'; | ||
|
||
export type OperationalErrorOptions = Omit<BaseErrorOptions, 'level'> & { | ||
level?: 'info' | 'warning' | 'error'; | ||
}; | ||
|
||
/** | ||
* Error that indicates a transient issue, like a network request failing, | ||
* a database query timing out, etc. These are expected to happen and should | ||
* be handled gracefully. | ||
* | ||
* Default level: warning | ||
* Default shouldReport: false | ||
*/ | ||
export class OperationalError extends BaseError { | ||
constructor(message: string, opts: OperationalErrorOptions = {}) { | ||
opts.level = opts.level ?? 'warning'; | ||
opts.shouldReport = opts.shouldReport ?? false; | ||
|
||
super(message, opts); | ||
} | ||
} |
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,23 @@ | ||
import type { BaseErrorOptions } from '@/errors/base/base.error'; | ||
import { BaseError } from '@/errors/base/base.error'; | ||
|
||
export type UnexpectedErrorOptions = Omit<BaseErrorOptions, 'level'> & { | ||
level?: 'error' | 'fatal'; | ||
}; | ||
|
||
/** | ||
* Error that indicates something is wrong in the code: logic mistakes, | ||
* unhandled cases, assertions that fail. These are not recoverable and | ||
* should be brought to developers' attention. | ||
* | ||
* Default level: error | ||
* Default shouldReport: true | ||
*/ | ||
export class UnexpectedError extends BaseError { | ||
constructor(message: string, opts: UnexpectedErrorOptions = {}) { | ||
opts.level = opts.level ?? 'error'; | ||
opts.shouldReport = opts.shouldReport ?? true; | ||
|
||
super(message, opts); | ||
} | ||
} |
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,26 @@ | ||
import type { BaseErrorOptions } from '@/errors/base/base.error'; | ||
import { BaseError } from '@/errors/base/base.error'; | ||
|
||
export type UserErrorOptions = Omit<BaseErrorOptions, 'level'> & { | ||
level?: 'info' | 'warning'; | ||
description?: string | null | undefined; | ||
}; | ||
|
||
/** | ||
* Error that indicates the user performed an action that caused an error. | ||
* E.g. provided invalid input, tried to access a resource they’re not | ||
* authorized to, or violates a business rule. | ||
* | ||
* Default level: info | ||
* Default shouldReport: false | ||
*/ | ||
export class UserError extends BaseError { | ||
readonly description: string | null | undefined; | ||
|
||
constructor(message: string, opts: UserErrorOptions = {}) { | ||
opts.level = opts.level ?? 'info'; | ||
opts.shouldReport = opts.shouldReport ?? false; | ||
|
||
super(message, opts); | ||
} | ||
} |
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,13 @@ | ||
import type { Event } from '@sentry/node'; | ||
|
||
export type ErrorLevel = 'warning' | 'error' | 'fatal' | 'info'; | ||
|
||
export type ErrorTags = NonNullable<Event['tags']>; | ||
|
||
export type ReportingOptions = { | ||
/** Whether the error should be reported to Sentry */ | ||
shouldReport?: boolean; | ||
level?: ErrorLevel; | ||
tags?: ErrorTags; | ||
extra?: Event['extra']; | ||
}; |
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