-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
133 additions
and
29 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 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,17 +1,15 @@ | ||
import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common'; | ||
import { DomainException } from './exception/domain-exception'; | ||
import { Request, Response } from 'express'; | ||
import { ErrorResponse } from './common/error-response'; | ||
import { JsonRpcExceptionFilter } from './json-rpc/json-rpc.decorator'; | ||
import { ExceptionFilter } from './json-rpc/exceptions/exception-filter'; | ||
|
||
@Catch(DomainException) | ||
export class DomainExceptionFilter implements ExceptionFilter { | ||
catch(exception: DomainException, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
const response = ctx.getResponse<Response>(); | ||
const request = ctx.getRequest<Request>(); | ||
|
||
response | ||
.status(400) | ||
.json(ErrorResponse.of(exception.errorData, exception.errorData.message)); | ||
@JsonRpcExceptionFilter(DomainException) | ||
export class DomainExceptionFilter implements ExceptionFilter<DomainException> { | ||
catch(exception: DomainException, callback: any) { | ||
callback({ | ||
code: -100, | ||
message: exception.errorData.message, | ||
errorData: exception.errorData, | ||
data: exception.data, | ||
}); | ||
} | ||
} |
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,15 @@ | ||
import { JsonRpcExceptionFilter } from './json-rpc.decorator'; | ||
import { JsonRpcError } from './error'; | ||
import { ExceptionFilter } from './exceptions/exception-filter'; | ||
import { JSONRPCCallbackType } from 'jayson'; | ||
|
||
@JsonRpcExceptionFilter(Error) | ||
export class JsonRpcDefaultExceptionFilter implements ExceptionFilter<Error> { | ||
catch(exception: Error, callback: JSONRPCCallbackType): void { | ||
if (exception instanceof JsonRpcError) { | ||
callback(exception); | ||
} else { | ||
throw exception; | ||
} | ||
} | ||
} |
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,8 @@ | ||
import * as jayson from 'jayson'; | ||
|
||
export interface ExceptionFilter<T extends Error> { | ||
catch( | ||
exception: T, | ||
callback: jayson.JSONRPCCallbackType | ||
): Promise<void> | void; | ||
} |
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,33 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ExceptionFilter } from './exception-filter'; | ||
import { JSONRPCCallbackType } from 'jayson'; | ||
|
||
export type JsonRpcExceptionHandlerMap<T extends Error> = { | ||
[exceptionType: string]: ExceptionFilter<T>; | ||
}; | ||
|
||
@Injectable() | ||
export class JsonRpcExceptionHandler<T extends Error> { | ||
private exceptionFilters: JsonRpcExceptionHandlerMap<any> = {}; | ||
|
||
constructor() {} | ||
|
||
registerFilter(exception: string, filter: ExceptionFilter<T>): void { | ||
this.exceptionFilters[exception] = filter; | ||
} | ||
|
||
handle(exception: T, callback: JSONRPCCallbackType): void { | ||
const exceptionType = Object.getPrototypeOf(exception).constructor.name; | ||
const filter = this.exceptionFilters[exceptionType]; | ||
|
||
if (!filter) { | ||
throw exception; | ||
} | ||
|
||
try { | ||
filter.catch(exception, callback); | ||
} catch (e) { | ||
callback(e, e); | ||
} | ||
} | ||
} |
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
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