JSON parse error - `Unexpected token 'N', "No connect"... is not valid JSON`
mirkobertone opened this issue · 2 comments
mirkobertone commented
Hi!
I have an architecture with an apigateway(grpc client) and a microservice(grpc server) .
When the grpc server is down, the GrpcToHttpInterceptor in controller catch this error:
{
code: 14,
details: "No connection established. Last error: connect ECONNREFUSED 0.0.0.0:50051 (2024-04-09T08:38:21.111Z)",
metadata: {
internalRepr: {
},
options: {
},
},
}
The JSON.parse(err.details)
brake in this case. Is it possible the if statement isn't checking in the right way?
I've also check with debug and I attach a screen
Here my controller in apigateway and interceptor
@UseInterceptors(GrpcToHttpInterceptor)
@Controller('/api/actors')
export class ActorController {
constructor(
private readonly actorsService: ApiActorService,
private readonly logger: WinstonLoggerService,
) {}
@Post()
@ApiOperation({ summary: 'Create an actor' })
create(@Body() actor: CreateActorRequest, @GetTraceId() traceID: string): Promise<ActorResponse> {
return this.actorsService.create(actor, { traceID });
}
}
mirkobertone commented
I created a PR if need it #14
rajibkuet07 commented
I think a proper solution for this issue would be to change the exception message in HttpToGrpcInterceptor
as follows-
return throwError(
() =>
new RpcException({
message: JSON.stringify({
error: exception.message,
type: typeof exception.message === 'string' ? 'string' : 'object',
exceptionName: RpcException.name,
}),
code: statusCode,
}),
);
As you are expecting an error object in the GrpcToHttpInterceptor
interceptor.
It would work for all sorts of HTTP exceptions from gRPC clients.