/symfony-skills:problem-details-rfc9457from symfony-skills
Generates RFC 9457 problem+json error responses in Symfony APIs via domain exceptions and a single kernel exception listener.
Ships in symfony-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Every error response is application/problem+json per RFC 9457:
{
"type": "https://api.example.com/problems/order-not-found",
"title": "Order not found",
"status": 404,
"detail": "No order exists with id 018f...",
"instance": "/api/orders/018f...",
"errors": [
{ "field": "items", "message": "This collection should contain 1 element or more." }
]
}
type — a stable URI identifying the problem class (dereferenceable docs ideally).title — short, human-readable, same for every occurrence of this type.status — the HTTP status code, duplicated in the body.detail — specific to this occurrence.instance — the request URI that produced it.errors, traceId, …) are allowed and encouraged.Define a base exception that knows its status and problem type. Throw it from services/domain; never build error JSON in a controller.
abstract class DomainException extends \RuntimeException
{
abstract public function statusCode(): int;
abstract public function problemType(): string;
public function title(): string
{
return (new \ReflectionClass($this))->getShortName();
}
}
final class OrderNotFoundException extends DomainException
{
public function __construct(Uuid $id)
{
parent::__construct("No order exists with id {$id->toRfc4122()}");
}
public function statusCode(): int { return Response::HTTP_NOT_FOUND; }
public function problemType(): string { return 'https://api.example.com/problems/order-not-found'; }
public function title(): string { return 'Order not found'; }
}
#[AsEventListener(event: KernelEvents::EXCEPTION, priority: 0)]
final class ProblemDetailsExceptionListener
{
public function __construct(
private readonly LoggerInterface $logger,
#[Autowire('%kernel.debug%')] private readonly bool $debug,
) {}
public function __invoke(ExceptionEvent $event): void
{
$e = $event->getThrowable();
$request = $event->getRequest();
$problem = match (true) {
$e instanceof DomainException => $this->fromDomain($e, $request),
$e instanceof ValidationFailedException => $this->fromValidation($e, $request),
$e instanceof HttpExceptionInterface => $this->fromHttp($e, $request),
default => $this->fromUnexpected($e, $request),
};
if ($problem['status'] >= 500) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
$event->setResponse(new JsonResponse(
$problem,
$problem['status'],
['Content-Type' => 'application/problem+json'],
));
}
private function fromDomain(DomainException $e, Request $request): array
{
return [
'type' => $e->problemType(),
'title' => $e->title(),
'status' => $e->statusCode(),
'detail' => $e->getMessage(),
'instance' => $request->getPathInfo(),
];
}
private function fromValidation(ValidationFailedException $e, Request $request): array
{
$errors = [];
foreach ($e->getViolations() as $v) {
$errors[] = ['field' => $v->getPropertyPath(), 'message' => $v->getMessage()];
}
return [
'type' => 'https://api.example.com/problems/validation-error',
'title' => 'Validation failed',
'status' => Response::HTTP_UNPROCESSABLE_ENTITY,
'detail' => 'The request payload did not pass validation.',
'instance' => $request->getPathInfo(),
'errors' => $errors,
];
}
private function fromHttp(HttpExceptionInterface $e, Request $request): array
{
return [
'type' => 'about:blank',
'title' => Response::$statusTexts[$e->getStatusCode()] ?? 'Error',
'status' => $e->getStatusCode(),
'detail' => $e->getMessage(),
'instance' => $request->getPathInfo(),
];
}
private function fromUnexpected(\Throwable $e, Request $request): array
{
return [
'type' => 'about:blank',
'title' => 'Internal Server Error',
'status' => Response::HTTP_INTERNAL_SERVER_ERROR,
// Never leak internals in prod.
'detail' => $this->debug ? $e->getMessage() : 'An unexpected error occurred.',
'instance' => $request->getPathInfo(),
];
}
}
prod, the detail for 5xx must be generic — never the exception message or stack trace.traceId; return that id so support can correlate.API Platform 4 already emits problem+json. You only need this listener for non-API-Platform controllers, or to customize the problem type/extensions. Map domain exceptions to status codes via config/packages/api_platform.yaml exception_to_status.
['error' => '...'] ad-hoc JSON — use the RFC 9457 shape with type/title/status/detail/instance.Content-Type: application/json for errors — use application/problem+json.try/catches in each controller — centralize in one KernelEvents::EXCEPTION listener.$e->getMessage() / stack traces on 5xx in prod — gate behind kernel.debug.400 for validation errors — Symfony convention is 422 Unprocessable Entity.DomainException base.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
problem-details, error-format, exception-listener, symfony-api
17 skills in this plugin
/api-platform-resourcesUse when exposing data through API Platform 4 — defining #[ApiResource], operations, DTO resources, state providers/processors, serialization groups, filters, and pagination. Use when the task mentions API Platform, ApiResource, state processor, or exposing an entity as a REST/GraphQL endpoint.
/bundle-organizationUse when structuring a Symfony project's src/ directory, organizing code into modules, configuring services, or deciding where a class belongs. Covers the modular monolith layout, service configuration, and config/ conventions for Symfony 7 (Flex, no app bundle).
/console-commandsUse when creating or refactoring Symfony console commands — never extends Command. On Symfony 8.1+ put #[AsCommand] on a method of a service class (even for a single command); on 7.x use the invokable attribute class. Also covers typed CLI inputs via value resolvers and #[MapInput] DTOs. Use when the task mentions console command, #[AsCommand], bin/console, #[Argument], #[Option], or a CLI task.
/doctrine-migrationsUse when changing the database schema in Symfony — generating Doctrine migrations, writing reversible up()/down(), data migrations, and deploy-safe (zero-downtime) changes. Use when the task mentions migration, schema change, ALTER TABLE, or doctrine:migrations.
/doctrine-ormUse when creating Doctrine ORM 3 entities, associations, repositories, enums, or UUID identifiers in Symfony. Defines entity conventions, attribute mapping, fetch strategy, and repository structure. Use when the task mentions entity, Doctrine, repository, association, or mapping.
/doctrine-query-optimizationUse when writing Doctrine queries, fixing N+1 problems, paginating large result sets, building read models, or tuning DQL/query builder performance. Use when the task mentions N+1, slow query, pagination, DQL, hydration, or read model.
npx claudepluginhub fatonh/symfony-skills --plugin symfony-skillsOther plugins' skills for the same job