Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Swallow GraphQL errors by Express #803

Open
Yoann-Abbes opened this issue Apr 28, 2022 · 6 comments
Open

Swallow GraphQL errors by Express #803

Yoann-Abbes opened this issue Apr 28, 2022 · 6 comments

Comments

@Yoann-Abbes
Copy link

Hi

I'm having issues using a simple express error handler middleware to swallow errors thrown in GraphQL resolvers

For example, I would have:

GQLside

function gqlQuery {
    if (!something) {
      throw new Error('Some Error');
    }

Express side

app.use(
      '/graphql',
     {
      schema,
      context,
      graphiql: true,
      customFormatErrorFn: (error: any) => {
        const errParams = {
          message: error.message,
          locations: error.locations,
          stack: error.stack,
        };
        return errParams;
      },
);

app.use((err, req, res, next) => {
      logger.info('Express middleware error handler')
      res.sendStatus(200)
      next();
    });

What I need, is when inside the resolver I throw the Some Error, Express swallows it inside it's middleware.
It does not work

The only way I managed to do it, is by throwing inside the customFormatErrorFn function:

customFormatErrorFn: (error: any) => {
        const errParams = {
          message: error.message,
          locations: error.locations,
          stack: error.stack,
        };
        if (error.originalError.message.startsWith('Some Error')) {
          throw error;
        }

If I do that, the Express middleware is being called.

But GraphQL returns a bad formatted response

{
  "message": "Unexpected token O in JSON at position 0",
  "stack": "SyntaxError: Unexpected token O in JSON at position 0"
}

I don't understand how I can simply manage GraphQL errors through Express. I'm certainly doing something wrong

My final goal is to catch those errors before they are sent to the client (to chose if I log them for example)

@divmgl
Copy link

divmgl commented Nov 28, 2022

So this issue was opened in April. Did you find a solution to this? I'm running into this right now and it's looking pretty dire.

@saihaj
Copy link
Member

saihaj commented Nov 28, 2022

@divmgl this project is deprecated. Checkout #817

@Yoann-Abbes
Copy link
Author

@divmgl No sorry, I let i go
If you find a solution I would be interested too

@mxmaxime
Copy link

Hi,
Exact same situation here.
I was digging but I think I'll migrate my code to remove this dependency :/

@stevenkissack
Copy link

Trying my best to understand the exact scenario you are trying to solve & I know this library is deprecated but I run it in production with graphql-shield and battled with error handling myself so much that I patched it for our use case. Some possibly helpful tips although untested:

Instead of filtering based on the message, use a custom error with an errorType like so:

import { CustomError } from 'ts-custom-error'

export class ApiError extends CustomError {
  public errorType: string
  public constructor(
    public code: string,
    message: string,
    public originalError?: Error,
  ) {
    super(message)
    this.errorType = 'ApiError'
  }
}

function someResolver() {
  throw new ApiError('oh_no', 'Oh No!', optionalOriginalErrorHere)
}

This will come through to your customFormatErrorFn, and you can then handle it based on the type. The next part is returning a valid GraphQL response. Look at the function graphqlMiddleware inside the the library index.ts and you will see how they construct it:

result = { data: undefined, errors: error.graphqlErrors }

I personally have a lot more custom logic painfully wired in to the library, but if you're stuck using this library, maybe it will help you find the right answer for you

@enisdenjo
Copy link
Member

This library has been deprecated and this repo will be archived soon. It has been superseded by graphql-http.

Furthermore, if you seek a fully-featured, well-maintained and performant server - I heavily recommend GraphQL Yoga!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants