Skip to content

Custom field not exposed in ErrorResource domain exception #7167

Open
@devDzign

Description

@devDzign

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions