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?

1 Like

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 :

2 Likes

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?

1 Like

@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.

2 Likes

I’ve been searching and trying various solutions for 2 days and finally stumbled across this and was able to complete my task. It would be very helpful if RedwoodGraphQLError were documented.

Hi @LarkSoftware am glad you found the solution and was wondering what could have helped you find it in the docs.

RedwoodError is documented in GraphQL | RedwoodJS Docs which is redwood/errors.ts at main · redwoodjs/redwood · GitHub.

GraphQL needs errors to be GraphQLErrors to be shown (otherwise all errors are masked to not leak info.

Redwood does this via a plugin to convert redwoodError to GraphQLErrors here: redwood/useRedwoodError.ts at main · redwoodjs/redwood · GitHub

Therefore, like the docs say, you just need to throw a RedwoodError and you message will be passed down in the response.

But:

To use a Redwood Error, import each from @redwoodjs/graphql-server.

* SyntaxError - An unspecified error occurred
* ValidationError - Invalid input to a service
* AuthenticationError - Failed to authenticate
* ForbiddenError - Unauthorized to access
* UserInputError - Missing input to a service

while there are RedwoodGraphQLError that’s specific to the graphql-server package.

If you were in your api and a service or a lib, you’d want to throw RedwoodError since it is an error and doesn’t necessarily have anything to do with GraphQL.

If you can think of a way to better document this, happy to have a PR or if you can suggest some text, I can add that, too.

See: redwood/graphql.md at main · redwoodjs/redwood · GitHub

Or should this be a separate section on errors?

Hi @LarkSoftware am glad you found the solution and was wondering what could have helped you find it in the docs.

Yeah I’m not sure. I’m still new to GraphQL so I was trying to figure out how to return a custom status code like a 500 and wasn’t getting anywhere.

RedwoodError is documented in GraphQL | RedwoodJS Docs which is redwood/errors.ts at main · redwoodjs/redwood · GitHub.

This is probably what I really want. I guess I wasn’t really looking for a GraphQL solution but rather a rest solution since that is what I’m used to. I don’t recall now how I actually landed on this page. I think I just started pasting strings from the errors I was getting.

Thank you for the information though, this is very helpful!

With RedwoodJS v4 and GraphQL Yoga v3, one can now add the status codes to the error extension data – but it may have some side effects with RedwoodJS’s form error handling.

Typically, errors in GraphQL are still status 200 and report the error message; i.e., they don’t have 400’s with bad request like a rest api might.

You can see a draft PR I began while exploring status codes for the error messages here: Draft: feature: Include appropriate HTTP Status Codes for Redwood Service Validations and GraphQL Errors by dthyresson · Pull Request #7293 · redwoodjs/redwood · GitHub

I held off as some form error handling changes might be needed – and really I was looking to use proper error codes with the SOFA rest api plugin (and there may be a different way to do that than this).

1 Like