Skip to content
Snippets Groups Projects
Select Git revision
  • e2b7cf318a97caa2b4265a8ca83cf8b6b42d306e
  • main default protected
  • register-logging-channel
  • expr-lang
  • ci-82
  • attr-events
  • locale-wip
  • custom-routes
  • v0.1.85
  • v0.1.84
  • v0.1.83
  • v0.1.82
  • v0.1.81
  • v0.1.80
  • v0.1.79
  • v0.1.78
  • v0.1.77
  • v0.1.76
  • v0.1.75
  • v0.1.74
  • v0.1.73
  • v0.1.72
  • v0.1.71
  • v0.1.70
  • v0.1.69
  • v0.1.68
  • v0.1.67
  • v0.1.65
28 results

errors.md

Blame
  • Christoph Reiter's avatar
    Reiter, Christoph authored
    So we can include them as a submodule in the dev-guide
    e2b7cf31
    History
    errors.md 1.57 KiB

    API Errors and Error Handling

    By default Symfony and API Platform convert HttpException and all subclasses to HTTP errors with a matching status code. See https://api-platform.com/docs/core/errors for details.

    Since API Platform by default hides any message details for >= 500 and < 600 in production and doesn't allow injecting any extra information into the resulting JSON-LD error response we provide a special HttpException subclass which provides those features.

    The following will pass the error message to the client even in case the status code is 5xx. Note that you have to be careful to not include any secrets in the error message since they would be exposed to the client.

    use Dbp\Relay\CoreBundle\Exception\ApiError;
    
    throw new APIError(500, 'My custom message');

    which results in:

    {
      "@context": "/contexts/Error",
      "@type": "hydra:Error",
      "hydra:title": "An error occurred",
      "hydra:description": "My custom message"
    }

    Further more you can include extra information like an error ID and some extra information in form of an object:

    use Dbp\Relay\CoreBundle\Exception\ApiError;
    
    throw new APIError::withDetails(500, 'My custom message', 'my-id', ['foo' => 42]);

    which results in:

    {
      "@context": "/contexts/Error",
      "@type": "hydra:Error",
      "hydra:title": "An error occurred",
      "hydra:description": "My custom message",
      "relay:errorId": "my-id",
      "relay:errorDetails": {
        "foo": 42
      }

    If you are using status codes <= 400 and are fine with just the message, then using any of the builtin exception types is fine.