Apollo Server : throwing an error inside context always returns an http 400 error on the client
Asked Answered
L

1

6

So, i got an apollo server running and an apollo-client in my iOS app.

I'm trying to implement an authentication process. And following apollo's documentation about authorization and authentication, i ended up trying the code they provided :


context: ({ req }) => {
    throw new AuthenticationError('you must be logged in');
},

But, while testing the code, i discovered that throwing an error wether it is a javascript one, or an apollo error, it always sends back an http 400 error :

Error: Response not successful: Received status code 400
    at new ApolloError (errors.cjs.js:31)
    at core.cjs.js:1493
    at both (utilities.cjs.js:963)
    at utilities.cjs.js:956
    at tryCallTwo (core.js:45)
    at doResolve (core.js:200)
    at new Promise (core.js:66)
    at Object.then (utilities.cjs.js:956)
    at Object.error (utilities.cjs.js:964)
    at notifySubscription (Observable.js:140)

The authentication error is not sent back. Even when i throw a custom error, with custom status code and everything it always returns the same error.

Am i doing something wrong ? Or is this an issue ?

Lanfranc answered 14/7, 2021 at 12:20 Comment(0)
J
2

If you do not count on receiving a 401 error code for any downstream tasks, I don't think it is an issue you need to worry about. However, if you do need to receive the expected 401, you can use Apollo Link for error handling. Read about it here: https://www.apollographql.com/docs/react/data/error-handling/#advanced-error-handling-with-apollo-link

If you still cannot fix the problem, browsing through this issue may also be helpful: https://github.com/apollographql/apollo-server/issues/1709

Hope this helps, happy coding :)

Janusfaced answered 15/7, 2021 at 19:8 Comment(1)
Thank you for your answer, i apologize for not answering sooner :)Lanfranc

© 2022 - 2024 — McMap. All rights reserved.