Graphql-laravel: Error Messaging

Created on 31 Dec 2019  路  3Comments  路  Source: rebing/graphql-laravel

Versions:

  • graphql-laravel Version: ^4.0
  • PHP Version: 7.1.29

Question:

Hey, happy new year!

I'm wondering what the go is about error messaging outside of validation, and what can be done in terms of throwing or returning specific errors.

Say you have a User Update Email Mutation, and the email field is unique (classic). There are a couple of things that could go wrong:

  • User not found by ID
  • Email already in use

Following the example, for user not found it would just return null (which I guess is common for GraphQL I'm still letting go of HTTP status codes). Now for the dupe email, I could also return null, but then the client won't know the difference. If I let the DB take the brunt we'll end up with "Internal Server Error", which is _true_ but then what's the difference between a legit bug and a unique constraint violation... and still the client couldn't inform the user either way.

So I'm just wondering before I spin my wheels on solving this if there's already a format in place for returning specific error types. Also I found that even when APP_ENV=production, exception responses return the whole stack trace, wondering if there's a way to disable this. Thank you very much in advance!

question

Most helpful comment

Just want to add to this in case someone else comes by : turns out webonyx/graphql-php gives you an interface for custom exceptions.

use GraphQL\Error\ClientAware;

class ResourceNotFoundException extends \Exception implements ClientAware {

    protected $message = "Resource not found";

    public function isClientSafe() {
        return true;
    }

    public function getCategory() {
        return 'resource_not_found';
    }
}

When this exception is thrown, the category and message are in the response:

{
  "errors": [
    {
      "message": "Resource not found",
      "extensions": {
        "category": "resource_not_found"
      },
      ...
}

https://webonyx.github.io/graphql-php/error-handling/

Cheers :-)

All 3 comments

Hey, happy new year!

馃コ

You may not like the/my answer, but: it's up to you!

To give some pointers:

  • you can use error_formatter to define _how_ the exception/error response looks like
  • you can use errors_handler how you want to treat exceptions (wrap them, swap them, ignore them, rethrow a different one)
  • you can return an additional field from your mutation return type which indicates whether a unique violation, e.g. email already exists, was encountered, if you want to "more gently" process this in the frontend
  • I've also seen cases where a query/mutation simple can return different shapes based on "what hapens", i.e. a union and then we either return a "success response type" or an "error response type" (which usually means catching the exception and return an appropriate value back, etc.).

I'm still letting go of HTTP status codes)

At the moment here's no defined _standard_ for this. GraphQL is transport agnostic. Of course HTTP(S) is the most used one. If you're interested, there's WG for that => https://github.com/APIs-guru/graphql-over-http

Also I found that even when APP_ENV=production, exception responses return the whole stack trace, wondering if there's a way to disable this. Thank you very much in advance!

That sounds a like a bug. However I can't reproduce it, I've projects using this in production and they work correctly. Before filing an issue here, I suggest to review your code and otherwise create a reproducible repository first.

Thanks @mfn, that's a very helpful response!

That sounds a like a bug.

It's being plugged into a legacy codebase, I would assume the bug is on my side. At least I know for sure now what the expected behavior is haha.

Just want to add to this in case someone else comes by : turns out webonyx/graphql-php gives you an interface for custom exceptions.

use GraphQL\Error\ClientAware;

class ResourceNotFoundException extends \Exception implements ClientAware {

    protected $message = "Resource not found";

    public function isClientSafe() {
        return true;
    }

    public function getCategory() {
        return 'resource_not_found';
    }
}

When this exception is thrown, the category and message are in the response:

{
  "errors": [
    {
      "message": "Resource not found",
      "extensions": {
        "category": "resource_not_found"
      },
      ...
}

https://webonyx.github.io/graphql-php/error-handling/

Cheers :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nozols picture nozols  路  8Comments

drmax24 picture drmax24  路  6Comments

jeekymojica picture jeekymojica  路  7Comments

niiapa picture niiapa  路  8Comments

stevelacey picture stevelacey  路  8Comments