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

Unable to set custom headers on errors #3062

Closed
ustun opened this issue Jul 19, 2019 · 4 comments
Closed

Unable to set custom headers on errors #3062

ustun opened this issue Jul 19, 2019 · 4 comments

Comments

@ustun
Copy link

ustun commented Jul 19, 2019

The headers for errors seem to be fixed, one cannot set any headers for errors via any other means (extensions etc).

See

const defaultHeaders = { 'Content-Type': 'application/json' };

The use case is explained in this issue: #3061

Note that I was able to send response headers for successful responses there, but not for errors.

@abernix
Copy link
Member

abernix commented Aug 26, 2019

Please try this with the new implementation provided in #2719 and let us know if it's not possible.

Roughly, I think you'd want this:

import { ApolloServer } from 'apollo-server';
const server = new ApolloServer({
  typeDefs,
  resolvers,
  plugins: [
    {
      requestDidStart() {
        return {
          didEncounterErrors({ response, errors }) {
            // You can inspect `errors`, if you'd like!
            response.http.headers.set('Your-Own-Header', 'SEVERITY!');
          }
        }
      },
    },
  ],
});

@abernix abernix closed this as completed Aug 26, 2019
@ustun
Copy link
Author

ustun commented Aug 27, 2019

Hmm, it seems that there are different categories of errors.

I was already catching errors in resolvers and the new plugin also worked for errors in resolvers, however unfortunately that didn't do the trick for parsing or validation errors, e.g. when we send a malformed query from the client.

When a validation error is detected (you ask for a field that is not in typedef), didEncounterErrors function is executed, however that response with headers is ignored, at least for the apollo-server-express.

@PrimarchAlpharius
Copy link

PrimarchAlpharius commented Jan 30, 2020

@abernix The proposed solution does not work.

In my case, the error (PersistedQueryNotFound) is picked up by didEncounterErrors, but setting of the header is apparently ignored.

At the moment, it appears impossible to customize the headers of (at least some) error responses.
Example, a persisted GET request that yields a PersistedQueryNotFoundError will go through didEncounterError (where setting the header simply does not work and the documentation for this event is next to non-existent) and formatError, where the context is not available.

Is there a way to accomplish this that I am not aware of? If not, would you consider adding support for this? Either allowing it in didEncounterError, or maybe adding a new event willSendErrorResponse (similar to willSendResponse)?

@msdinit
Copy link

msdinit commented Mar 9, 2020

For anyone trying to resolve this, the problem seems to be that for some errors (namely APQ errors), apollo server forces some pre-defined headers. A temporary workaround is to manually throw an HttpQueryError with your own headers:

didEncounterErrors ({ errors, debug }) {
    if (hasPersistedQueryError(errors)) {
        throw new HttpQueryError(
            200,
            JSON.stringify({ errors: formatApolloErrors(errors, { debug: debug }) }),
            true,
            { 'Cache-Control': 'max-age=0, stale-while-revalidate' }
        )
    }
}

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
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

4 participants