Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON parse error - Unexpected token 'N', "No connect"... is not valid JSON #13

Open
mirkobertone opened this issue Apr 9, 2024 · 2 comments

Comments

@mirkobertone
Copy link

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

image

image

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
Copy link
Author

mirkobertone commented Apr 9, 2024

I created a PR if need it #14

@rajibkuet07
Copy link

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.

rajibkuet07 added a commit to rajibkuet07/nestjs-grpc-exceptions that referenced this issue Jun 7, 2024
Handles all sorts of http exceptions from rpc client.
No need to use extra try catch for parse error in grpc to http interceptor.

Refs: mohsenbostan#13
Refs: mohsenbostan#14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants