- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat(artusx-koa): update exception api
- v1.1.5-rc.14
- v1.1.5-rc.13
- v1.1.5-rc.12
- v1.1.5-rc.11
- v1.1.5-rc.10
- v1.1.5-rc.9
- v1.1.5-rc.8
- v1.1.5-rc.7
- v1.1.5-rc.6
- v1.1.5-rc.5
- v1.1.5-rc.4
- v1.1.5-rc.3
- v1.1.5-rc.2
- v1.1.5-19
- v1.1.5-18
- v1.1.5-17
- v1.1.5-16
- v1.1.5-15
- v1.1.5-14
- v1.1.5-13
- v1.1.5-12
- v1.1.5-11
- v1.1.5-10
- v1.1.5-9
- v1.1.5-8
- v1.1.5-7
- v1.1.5-6
- v1.1.5-5
- v1.1.5-4
- v1.1.5-3
- v1.1.5-2
- v1.1.5-1
- v1.1.5-0
- v1.1.4
- v1.1.4-rc.1
- v1.1.3
- v1.1.2
- v1.1.1
Showing
5 changed files
with
112 additions
and
6 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
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'; | ||
} |
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,6 @@ | ||
{ | ||
"BIZ:COMMON_ERROR": { | ||
"desc": "BIZ COMMON ERROR", | ||
"detailUrl": "https://github.com/artusjs/artusx" | ||
} | ||
} |
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,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); | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
packages/apps/artusx-koa/src/module-exception/exception.controller.ts
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,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'; | ||
} | ||
} |