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

Micro server not handling Network errors #1581

Closed
1 task done
ghost opened this issue Aug 26, 2018 · 2 comments · May be fixed by project-accelerate/accelerate#241
Closed
1 task done

Micro server not handling Network errors #1581

ghost opened this issue Aug 26, 2018 · 2 comments · May be fixed by project-accelerate/accelerate#241
Labels
🌹 has-reproduction ❤ Has a reproduction in Glitch, CodeSandbox or Git repository.

Comments

@ghost
Copy link

ghost commented Aug 26, 2018

**Updated below to issue with micro server *

When a mutation error occurs a JSON reponse with an error object is returned - the client can parse and handle this.
{data: {requestToken: null},errors: [{message: "Unknown User"}]

However, when a schema error or request error occurs, I get a string response which cannot be parsed client side.
HttpQueryError: {"errors":[{"message":"Variable.....}

This is using apollo-server-micro how should I catch these errors and provide a graceful message back to the client which could aide in debugging?

  • has-reproduction
@ghost ghost added the 🌹 has-reproduction ❤ Has a reproduction in Glitch, CodeSandbox or Git repository. label Aug 26, 2018
@ghost ghost changed the title How to Capture HttpQueryError on Client Micro server not handling Network errors Aug 27, 2018
@ghost ghost added the 🌹 has-reproduction ❤ Has a reproduction in Glitch, CodeSandbox or Git repository. label Aug 27, 2018
@ghost
Copy link
Author

ghost commented Aug 27, 2018

When I use a vanilla apollo-server, my client recieves query errors (as documented) in the following manner:

[GraphQL error]: Message: Schema is not configured for mutations.
[Network error]: Error: Response not successful: Received status code 400

When using Micro server, instead of the two errors, a string is returned and is not understood by Apollo Client:
HttpQueryError: {"errors":[{"message":"Variable.....}

To reproduce this - use the micro example and send an incorrect request.

@fromthemills
Copy link
Contributor

fromthemills commented Sep 5, 2018

Had the same problem. For me this was happening when an error was throw in the context function.

Something like:

context: () => {
    throw new AuthenticationError('\'jwtToken\' must be a string.')
}

Going through the source code I suspect the problem is in the top level micro error handling and supsquent response.

} catch (error) {
    if ('HttpQueryError' === error.name && error.headers) {
        setHeaders(res, error.headers);
    }

    if (!error.statusCode) {
        error.statusCode = 500;
    }

    throw error;
}

Instead of throwing an error, we should return the result of the message. The message contains the correct json to be return as the response body.

} catch (error) {
    if ('HttpQueryError' === error.name && error.headers) {
        setHeaders(res, error.headers);
    }

    if (!error.statusCode) {
        error.statusCode = 500;
    }

    send(res, error.statusCode, error.message);
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🌹 has-reproduction ❤ Has a reproduction in Glitch, CodeSandbox or Git repository.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant