11import * as express from "express" ;
22import * as http from "http" ;
33
4- import { Server , CustomTransportStrategy } from "@nestjs/microservices" ;
4+ import { Server , CustomTransportStrategy , RpcException } from "@nestjs/microservices" ;
55import { Injectable , Controller } from "@nestjs/common" ;
66import { MessagePattern } from "@nestjs/microservices" ;
77
@@ -21,8 +21,8 @@ export interface JSONRPCServerOptions {
2121 */
2222 hostname ?: string ;
2323 /*
24- * The path at which the JSON RPC endpoint should be mounted
25- */
24+ * The path at which the JSON RPC endpoint should be mounted
25+ */
2626 path : string ;
2727}
2828
@@ -74,7 +74,10 @@ export class JSONRPCServer extends Server implements CustomTransportStrategy {
7474 ) ;
7575
7676 if ( "error" in response ) {
77- res . status ( 500 ) . json ( { error : response . error . message } )
77+ let resp = { code : 500 , message : response . error . message , data : undefined } ;
78+ if ( "code" in response . error ) resp . code = response . error . code ;
79+ if ( "data" in response . error ) resp . data = response . error . data ;
80+ res . status ( resp . code ) . json ( resp ) ;
7881 } else {
7982 res . status ( 200 ) . json ( response . value ) ;
8083 }
@@ -96,3 +99,9 @@ export class JSONRPCServer extends Server implements CustomTransportStrategy {
9699 // do nothing, maybe block further requests
97100 }
98101}
102+
103+ export class CodedRpcException extends RpcException {
104+ constructor ( message : string , public code : number = 500 , public data : any = { } ) {
105+ super ( { message, code, data } ) ;
106+ }
107+ }
0 commit comments