-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(exception): static error class & remove exception handler (#208
- Loading branch information
1 parent
d500fb9
commit f7e615b
Showing
8 changed files
with
57 additions
and
106 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,42 @@ | ||
import { ARTUS_EXCEPTION_DEFAULT_LOCALE } from '../constant'; | ||
import { ExceptionItem } from './types'; | ||
|
||
export class ArtusStdError extends Error { | ||
name = 'ArtusStdError'; | ||
private _code: string; | ||
private static codeMap: Map<string, ExceptionItem> = new Map(); | ||
private static currentLocale: string = process.env.ARTUS_ERROR_LOCALE || ARTUS_EXCEPTION_DEFAULT_LOCALE; | ||
|
||
public static registerCode(code: string, item: ExceptionItem) { | ||
this.codeMap.set(code, item); | ||
} | ||
|
||
public static setCurrentLocale(locale: string) { | ||
this.currentLocale = locale; | ||
} | ||
|
||
constructor (code: string) { | ||
super(`[${code}] This is Artus standard error, Please check on https://github.com/artusjs/error-code`); | ||
this._code = code; | ||
} | ||
|
||
public get code(): string { | ||
return this._code; | ||
} | ||
|
||
public get desc(): string { | ||
const { codeMap, currentLocale } = ArtusStdError; | ||
const exceptionItem = codeMap.get(this._code); | ||
if (!exceptionItem) { | ||
return 'Unknown Error'; | ||
} | ||
if (typeof exceptionItem.desc === 'string') { | ||
return exceptionItem.desc; | ||
} | ||
return exceptionItem.desc[currentLocale]; | ||
} | ||
|
||
public get detailUrl(): string|undefined { | ||
return ArtusStdError.codeMap.get(this._code)?.detailUrl; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1 @@ | ||
import ExceptionHandler from './handler'; | ||
|
||
export * from './error'; | ||
|
||
export { | ||
ExceptionHandler, | ||
}; | ||
export * from './impl'; |
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