Skip to content

Use values from ProblemExceptionInterface#7776

Merged
soyuka merged 4 commits intoapi-platform:mainfrom
bonroyage:problem-exception-interface
Feb 18, 2026
Merged

Use values from ProblemExceptionInterface#7776
soyuka merged 4 commits intoapi-platform:mainfrom
bonroyage:problem-exception-interface

Conversation

@bonroyage
Copy link
Contributor

Q A
Branch? main
Tickets None
License MIT
Doc PR

I have a couple of custom exceptions defined that all implement the ProblemExceptionInterface (as described in the documentation). However, in the response they're still rendered as follows. I want the values to be shown that I have defined in the methods required by the interface.

throw new TeapotException('Exception message be here');
{
    "type": "/errors/500",
    "title": "An error occurred",
    "status": 500,
    "detail": "Exception message be here"
}

I dove into the ErrorHandler that's defined for Laravel, and noticed that it always calls Error::createFromException($exception, $statusCode). It's this method that sets those values. In this PR, I added a condition that if the exception passed to it is an instance of ProblemExceptionInterface, then the Error will be created entirely using the values from that interface.

Following this change, the following exception generates the following output:

class TeapotException extends \Exception implements ProblemExceptionInterface
{
    public function getType(): string
    {
        return 'teapot';
    }

    public function getTitle(): ?string
    {
        return 'I am a teapot';
    }

    public function getStatus(): ?int
    {
        return 418;
    }

    public function getDetail(): ?string
    {
        return 'No coffee';
    }

    public function getInstance(): ?string
    {
        return null;
    }
}
{
    "type": "teapot",
    "title": "I am a teapot",
    "status": 418,
    "detail": "No coffee"
}

I'm not sure if you see this as a feature or a (breaking) bug fix, so feel free to correct me if necessary and I'll happily rebase it to the correct 4.x branch. Or perhaps there's another way to achieve this that I'm not seeing at the moment.

@soyuka
Copy link
Member

soyuka commented Feb 17, 2026

Mhhh interesting, Symfony uses

The use looks quite correct I like the patch.

Note to self:

public static function createFromException(\Exception|\Throwable $exception, int $status): self
{
$headers = ($exception instanceof SymfonyHttpExceptionInterface || $exception instanceof HttpExceptionInterface) ? $exception->getHeaders() : [];
return new self('An error occurred', $exception->getMessage(), $status, $exception->getTrace(), type: "/errors/$status", headers: $headers, previous: $exception->getPrevious());
}

It may also need fixing.

If you could add a test to JsonProblemTest it'd be great (informations on how to run tests are inside CONTRIBUTING.md)

@bonroyage
Copy link
Contributor Author

Do you want me to update core/src/State/ApiResource/Error.php as well with the same change?

Would you consider this a feature or bug fix? Knowing that, I can potentially rebase, and also fix the first commit message to satisfy commitlint.

@soyuka
Copy link
Member

soyuka commented Feb 18, 2026

Do you want me to update core/src/State/ApiResource/Error.php as well with the same change?

Indeed this would be nice. We can leave that on the main branch as a feature, 4.3 is a few weeks out.

@bonroyage bonroyage force-pushed the problem-exception-interface branch from 0d0a740 to 9e5e095 Compare February 18, 2026 10:53
@bonroyage bonroyage force-pushed the problem-exception-interface branch from 9e5e095 to 2a6bd07 Compare February 18, 2026 10:55
@soyuka soyuka merged commit 6626549 into api-platform:main Feb 18, 2026
148 of 149 checks passed
@soyuka
Copy link
Member

soyuka commented Feb 18, 2026

thanks!

@bonroyage bonroyage deleted the problem-exception-interface branch February 18, 2026 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments