core icon indicating copy to clipboard operation
core copied to clipboard

Custom field not exposed in ErrorResource domain exception

Open devDzign opened this issue 9 months ago • 0 comments

Hello,

I am currently trying to follow the API Platform documentation to create a custom domain exception using #[ErrorResource].

My goal is to expose a custom field to API clients when an exception is thrown.

Here's my code: `use ApiPlatform\Metadata\ErrorResource; use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;

#[ErrorResource] class MyDomainException extends \Exception implements ProblemExceptionInterface { public function getType(): string { return '/errors/418'; }

public function getTitle(): ?string
{
    return 'Teapot error';
}

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

public function getDetail(): ?string
{
    return $this->getMessage();
}

public function getInstance(): ?string
{
    return null;
}

public string $myCustomField = 'I usually prefer coffee.';

} ` However, when this exception is thrown and caught by API Platform, the myCustomField is not present in the JSON response. I expected it to be exposed in the output, but it is not.

Is there a way to make this custom property appear in the error response? Do I need to use serialization groups or a custom normalizer?

Thank you for your help!

devDzign avatar May 22 '25 09:05 devDzign