From f7e615bc324ce8fd7f8f9289bef93c748d7127c1 Mon Sep 17 00:00:00 2001 From: Noah Gao Date: Wed, 19 Oct 2022 14:45:04 +0800 Subject: [PATCH] refactor(exception): static error class & remove exception handler (#208) --- src/application.ts | 11 +++------- src/exception/error.ts | 41 ----------------------------------- src/exception/handler.ts | 24 --------------------- src/exception/impl.ts | 42 ++++++++++++++++++++++++++++++++++++ src/exception/index.ts | 8 +------ src/loader/impl/exception.ts | 12 ++--------- test/app.test.ts | 3 +-- test/exception.test.ts | 22 +++++++------------ 8 files changed, 57 insertions(+), 106 deletions(-) delete mode 100644 src/exception/error.ts delete mode 100644 src/exception/handler.ts create mode 100644 src/exception/impl.ts diff --git a/src/application.ts b/src/application.ts index 31c6bfb..e0dcf1a 100644 --- a/src/application.ts +++ b/src/application.ts @@ -1,7 +1,7 @@ import 'reflect-metadata'; import { Container } from '@artus/injection'; import { ArtusInjectEnum } from './constant'; -import { ArtusStdError, ExceptionHandler } from './exception'; +import { ArtusStdError } from './exception'; import { HookFunction, LifecycleManager } from './lifecycle'; import { LoaderFactory, Manifest } from './loader'; import { Application, ApplicationInitOptions } from './types'; @@ -40,10 +40,6 @@ export class ArtusApplication implements Application { return this.container.get(ArtusInjectEnum.Packages); } - get exceptionHandler(): ExceptionHandler { - return this.container.get(ExceptionHandler); - } - get configurationHandler(): ConfigurationHandler { return this.container.get(ConfigurationHandler); } @@ -61,7 +57,6 @@ export class ArtusApplication implements Application { this.container.set({ type: ConfigurationHandler }); this.container.set({ type: Logger }); this.container.set({ type: Trigger }); - this.container.set({ type: ExceptionHandler }); } async load(manifest: Manifest, root: string = process.cwd()) { @@ -96,11 +91,11 @@ export class ArtusApplication implements Application { } throwException(code: string): void { - this.exceptionHandler.throw(code); + throw new ArtusStdError(code); } createException(code: string): ArtusStdError { - return this.exceptionHandler.create(code); + return new ArtusStdError(code); } protected addLoaderListener() { diff --git a/src/exception/error.ts b/src/exception/error.ts deleted file mode 100644 index f58dc3e..0000000 --- a/src/exception/error.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { ARTUS_EXCEPTION_DEFAULT_LOCALE } from '../constant'; -import { ExceptionItem } from './types'; - -export const ErrorCodeUtils = { - getI18NDesc (codeMap: Map, code: string, locale?: string) { - const currentLocale = locale || process.env.ARTUS_ERROR_LOCALE || ARTUS_EXCEPTION_DEFAULT_LOCALE; - const exceptionItem = codeMap.get(code); - if (!exceptionItem) { - return 'Unknown Error'; - } - if (typeof exceptionItem.desc === 'string') { - return exceptionItem.desc; - } - return exceptionItem.desc[currentLocale]; - }, -}; - -export class ArtusStdError extends Error { - name = 'ArtusStdError'; - private _code: string; - private _codeMap: Map; - - constructor (code: string, codeMap: Map) { - super(`[${code}] This is Artus standard error, Please check on https://github.com/artusjs/error-code`); - this._code = code; - this._codeMap = codeMap; - } - - get code(): string { - return this._code; - } - - get desc(): string { - // TODO: 待支持从 Config 获取 I18N 的 Locale - return ErrorCodeUtils.getI18NDesc(this._codeMap, this._code); - } - - get detailUrl(): string|undefined { - return this._codeMap.get(this._code)?.detailUrl; - } -} \ No newline at end of file diff --git a/src/exception/handler.ts b/src/exception/handler.ts deleted file mode 100644 index 76ccfe6..0000000 --- a/src/exception/handler.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Injectable } from '@artus/injection'; -import { ArtusStdError } from './error'; -import { ExceptionItem } from './types'; - -@Injectable() -export default class ExceptionHandler { - private errorCodeMap: Map = new Map(); - - registerCode(code: string, exceptionItem: ExceptionItem): void { - if (this.errorCodeMap.has(code)) { - console.warn(`[Artus-Exception] Register error-code failed, code is existed.(${code})`); - return; - } - this.errorCodeMap.set(code, exceptionItem); - } - - throw(code: string): void { - throw new ArtusStdError(code, this.errorCodeMap); - } - - create(code: string): ArtusStdError { - return new ArtusStdError(code, this.errorCodeMap); - } -} diff --git a/src/exception/impl.ts b/src/exception/impl.ts new file mode 100644 index 0000000..a1bf7d2 --- /dev/null +++ b/src/exception/impl.ts @@ -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 = 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; + } +} diff --git a/src/exception/index.ts b/src/exception/index.ts index aad7804..4cabdf4 100644 --- a/src/exception/index.ts +++ b/src/exception/index.ts @@ -1,7 +1 @@ -import ExceptionHandler from './handler'; - -export * from './error'; - -export { - ExceptionHandler, -}; +export * from './impl'; diff --git a/src/loader/impl/exception.ts b/src/loader/impl/exception.ts index 473c80d..41c9f28 100644 --- a/src/loader/impl/exception.ts +++ b/src/loader/impl/exception.ts @@ -1,32 +1,24 @@ -import { Container } from '@artus/injection'; import { DefineLoader } from '../decorator'; import { ManifestItem, Loader, LoaderFindOptions } from '../types'; import { ExceptionItem } from '../../exception/types'; -import { ExceptionHandler } from '../../exception'; import { loadMetaFile } from '../../utils/load_meta_file'; import { EXCEPTION_FILENAME } from '../../constant'; import { isMatch } from '../../utils'; +import { ArtusStdError } from '../../exception'; @DefineLoader('exception') class ExceptionLoader implements Loader { - private container: Container; - - constructor(container) { - this.container = container; - } - static async is(opts: LoaderFindOptions) { return isMatch(opts.filename, EXCEPTION_FILENAME); } async load(item: ManifestItem) { - const exceptionHandler = this.container.get(ExceptionHandler); try { const codeMap: Record = await loadMetaFile< Record >(item.path); for (const [errCode, exceptionItem] of Object.entries(codeMap)) { - exceptionHandler.registerCode(errCode, exceptionItem); + ArtusStdError.registerCode(errCode, exceptionItem); } return codeMap; } catch (error) { diff --git a/test/app.test.ts b/test/app.test.ts index c5f39a2..1b0103e 100644 --- a/test/app.test.ts +++ b/test/app.test.ts @@ -1,7 +1,7 @@ import 'reflect-metadata'; import axios from 'axios'; import assert from 'assert'; -import { ArtusInjectEnum, ConfigurationHandler, ExceptionHandler } from '../src'; +import { ArtusInjectEnum, ConfigurationHandler } from '../src'; describe('test/app.test.ts', () => { describe('app koa with ts', () => { @@ -25,7 +25,6 @@ describe('test/app.test.ts', () => { // Check Artus Default Class Inject to Contianer expect(() => app.container.get(ArtusInjectEnum.Application)).not.toThrow(); expect(() => app.container.get(ArtusInjectEnum.LifecycleManager)).not.toThrow(); - expect(() => app.container.get(ExceptionHandler)).not.toThrow(); expect(() => app.container.get(ConfigurationHandler)).not.toThrow(); await main(); diff --git a/test/exception.test.ts b/test/exception.test.ts index 84eb7a3..5349b53 100644 --- a/test/exception.test.ts +++ b/test/exception.test.ts @@ -1,26 +1,25 @@ import 'reflect-metadata'; import assert from 'assert'; -import { ArtusStdError, ErrorCodeUtils, ExceptionHandler } from '../src/exception'; +import { ArtusStdError } from '../src/exception'; import { ExceptionItem } from '../src/exception/types'; describe('test/app.test.ts', () => { describe('register error code and throw', () => { - const exceptionHandler = new ExceptionHandler(); const errorCode = 'ARTUS:TEMP_TEST'; const exceptionItem: ExceptionItem = { desc: 'TEST-DESC', detailUrl: 'http://test.artusjs.org', }; - exceptionHandler.registerCode(errorCode, exceptionItem); + ArtusStdError.registerCode(errorCode, exceptionItem); try { - exceptionHandler.throw(errorCode); + throw new ArtusStdError(errorCode); } catch (error) { assert(error instanceof ArtusStdError); assert(error.code === errorCode); assert(error.desc === exceptionItem.desc); assert(error.detailUrl === exceptionItem.detailUrl); } - const error = exceptionHandler.create(errorCode); + const error = new ArtusStdError(errorCode); assert(error instanceof ArtusStdError); assert(error.code === errorCode); assert(error.desc === exceptionItem.desc); @@ -28,7 +27,6 @@ describe('test/app.test.ts', () => { }); describe('register error code and throw, with i18n', () => { - const exceptionHandler = new ExceptionHandler(); const errorCode = 'ARTUS:TEMP_TEST_I18N'; const exceptionItem: ExceptionItem = { desc: { @@ -37,29 +35,25 @@ describe('test/app.test.ts', () => { }, detailUrl: 'http://test.artusjs.org', }; - exceptionHandler.registerCode(errorCode, exceptionItem); + ArtusStdError.registerCode(errorCode, exceptionItem); [ undefined, 'zh', 'en', ].forEach(locale => { if (locale) { - process.env.ARTUS_ERROR_LOCALE = locale; + ArtusStdError.setCurrentLocale(locale); } const tDesc = exceptionItem.desc[locale || 'en']; - const tmpCodeMap: Map = new Map([ - [errorCode, exceptionItem], - ]); - assert(ErrorCodeUtils.getI18NDesc(tmpCodeMap, errorCode, locale) === tDesc); try { - exceptionHandler.throw(errorCode); + throw new ArtusStdError(errorCode); } catch (error) { assert(error instanceof ArtusStdError); assert(error.code === errorCode); assert(error.desc === tDesc); assert(error.detailUrl === exceptionItem.detailUrl); } - const error = exceptionHandler.create(errorCode); + const error = new ArtusStdError(errorCode); assert(error instanceof ArtusStdError); assert(error.code === errorCode); assert(error.desc === tDesc);