Services) Handling 404 error?

Newbie question, how do I handle 404 status error. I’m trying to speak with 3-party api and there could be a scenario when it gives back 404. What should I do?

What kind of exception should I throw back to the user?

How do I change this text

Usually I throw back a translation pointer and handle all messages in my front app.

On such a case I’d catch the exception to make sure it is a pointer my app understands that is being sent back - but you should make sure to prepare a proper strategy, errors should not just be hidden away.

GraphQL have the reputation of always give code 200, even when you got an error. The reason behind it is at the HTTP layer GraphQL works, everything is fine, but it’s at the GraphQL layer is where something went wrong. Does it make sense ? I don’t know. Another thing, is a good security practice to hide the real error for the client, so the attacker cannot see potential way to get what they want.

It is still possible to send custom error respond with RedwoodJS. You need first import the error handler :
import { RedwoodGraphQLError } from '@redwoodjs/graphql-server'
Inside your service and return your custom GraphQL error :
return new RedwoodGraphQLError('The error message', { code: 'YOUR_INTERNAL_CODE' })

Finally, I found a good article about GraphQL error handling :

1 Like

Yeah, I recommend going through this article too, it gives a really nice way of going about GraphQL Errors. She also has a talk 200 OK! Error Handling in GraphQL - YouTube

1 Like

@guledali that’s part of the masked errors plug-in on envelop that protects unexpected exceptions and their messages from leaking sensitive data.

I have a draft PR with some documentation for the error masking here that I need to get up on the site:

In short, if you raise one of the Redwood errors derived from GraphQLError then it’s message is shown.

Also I recently made a change that let’s you customize that message:

But need to check that this latest envelop is in 0.37 but if not then it will be in 0.38

1 Like

Thanks for the person who pointed out RedwoodGraphQLError exactly what I needed.

Why is RedwoodGraphQLError not mention in the docs?

@guledali adding docs for those GraphQL updates was on my plate and it slipped through my tasks.

There is a PR for that — and CORS config and health check and errors and error masking that will be deployed soon (or v38).

Thanks for understanding and also pointing out the missing gaps in docs. Lots to know and only by showing gaps will we improve them.