Skip to content

Commit

Permalink
refactor(exception): static error class & remove exception handler (#208
Browse files Browse the repository at this point in the history
)
  • Loading branch information
noahziheng authored Oct 19, 2022
1 parent d500fb9 commit f7e615b
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 106 deletions.
11 changes: 3 additions & 8 deletions src/application.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
}
Expand All @@ -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()) {
Expand Down Expand Up @@ -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() {
Expand Down
41 changes: 0 additions & 41 deletions src/exception/error.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/exception/handler.ts

This file was deleted.

42 changes: 42 additions & 0 deletions src/exception/impl.ts
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;
}
}
8 changes: 1 addition & 7 deletions src/exception/index.ts
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';
12 changes: 2 additions & 10 deletions src/loader/impl/exception.ts
Original file line number Diff line number Diff line change
@@ -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<string, ExceptionItem> = await loadMetaFile<
Record<string, ExceptionItem>
>(item.path);
for (const [errCode, exceptionItem] of Object.entries(codeMap)) {
exceptionHandler.registerCode(errCode, exceptionItem);
ArtusStdError.registerCode(errCode, exceptionItem);
}
return codeMap;
} catch (error) {
Expand Down
3 changes: 1 addition & 2 deletions test/app.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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();
Expand Down
22 changes: 8 additions & 14 deletions test/exception.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
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);
assert(error.detailUrl === exceptionItem.detailUrl);
});

describe('register error code and throw, with i18n', () => {
const exceptionHandler = new ExceptionHandler();
const errorCode = 'ARTUS:TEMP_TEST_I18N';
const exceptionItem: ExceptionItem = {
desc: {
Expand All @@ -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<string, ExceptionItem> = 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);
Expand Down

0 comments on commit f7e615b

Please sign in to comment.