Skip to content

Commit

Permalink
feat(artusx-koa): update exception api
Browse files Browse the repository at this point in the history
thonatos committed Apr 1, 2024

Verified

This commit was signed with the committer’s verified signature.
1 parent 3f35a68 commit 3504d58
Showing 5 changed files with 112 additions and 6 deletions.
14 changes: 14 additions & 0 deletions packages/apps/artusx-koa/src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ArtusStdError } from '@artusx/core';

export class ArtusXWrappedError extends ArtusStdError {
static code = 'ARTUSX:WRAPPED_ERROR';
name = 'ArtusXWrappedError';

constructor() {
super(ArtusXWrappedError.code);
}
}

export class BizCustomError extends Error {
name = 'BizCustomError';
}
6 changes: 6 additions & 0 deletions packages/apps/artusx-koa/src/exception.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"BIZ:COMMON_ERROR": {
"desc": "BIZ COMMON ERROR",
"detailUrl": "https://github.com/artusjs/artusx"
}
}
26 changes: 24 additions & 2 deletions packages/apps/artusx-koa/src/filter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
import { Catch } from '@artusx/core';
import { Catch, ArtusStdError } from '@artusx/core';
import { ExceptionFilterType } from '@artusx/core';
import { ArtusXWrappedError, BizCustomError } from './error';

@Catch()
export class DefaultExceptionHandler implements ExceptionFilterType {
async catch(_err: Error) {
console.log('DefaultExceptionHandler');
console.log('error:name', _err.name);
}
}

@Catch('BIZ:COMMON_ERROR')
export class AppCodeExceptionHandler implements ExceptionFilterType {
async catch(_err: ArtusStdError) {
console.log('error:code', _err.code);
}
}

@Catch(ArtusXWrappedError)
export class ArtusXWrapperExceptionHandler implements ExceptionFilterType {
async catch(_err: ArtusXWrappedError) {
console.log('error:code', _err.code);
}
}

@Catch(BizCustomError)
export class BizCustomExceptionHandler implements ExceptionFilterType {
async catch(_err: BizCustomError) {
console.log('error:name', _err.name);
}
}
4 changes: 0 additions & 4 deletions packages/apps/artusx-koa/src/module-api/api.controller.ts
Original file line number Diff line number Diff line change
@@ -15,10 +15,6 @@ export default class APIController {

@GET('/')
async home(ctx: ArtusXContext) {
const err = this.app.createException('ARTUSX:UNKNOWN_ERROR');
if (err) {
throw err;
}
ctx.body = 'api';
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { ArtusInjectEnum, Inject, GET, Controller, ArtusApplication, ArtusXStdError } from '@artusx/core';
import type { ArtusXContext } from '@artusx/core';
import { ArtusXWrappedError, BizCustomError } from '../error';

@Controller('/exception')
export default class APIController {
@Inject(ArtusInjectEnum.Application)
app: ArtusApplication;

@GET('/system_error')
async systemError(ctx: ArtusXContext) {
const passed = ctx.request.query?.passed;
if (!passed) {
this.app.throwException('ARTUSX:UNKNOWN_ERROR');
}
ctx.body = 'system_error';
}

@GET('/default_error')
async defaultError(ctx: ArtusXContext) {
const err = new Error('default error message');
err.name = 'default error';

if (err) {
throw err;
}
ctx.body = 'default_error';
}

@GET('/wrapper_error')
async wrapperError(ctx: ArtusXContext) {
const err = new ArtusXWrappedError();
if (err) {
throw err;
}
ctx.body = 'wrapper_error';
}

@GET('/std_error')
async stdError(ctx: ArtusXContext) {
const err = new ArtusXStdError(400, 'artusx std error', {
data: {},
});

if (err) {
throw err;
}
ctx.body = 'biz_custom_error';
}

@GET('/biz_common_error')
async commonError(ctx: ArtusXContext) {
const passed = ctx.request.query?.passed;
if (!passed) {
this.app.throwException('BIZ:COMMON_ERROR');
}
ctx.body = 'biz_common_error';
}

@GET('/biz_custom_error')
async customError(ctx: ArtusXContext) {
const err = new BizCustomError('biz custom error message');
if (err) {
throw err;
}
ctx.body = 'biz_custom_error';
}
}

0 comments on commit 3504d58

Please sign in to comment.