abc/api-problem
Lightweight PHP library for representing API errors using RFC 7807 “Problem Details for HTTP APIs”. Create ApiProblem instances with type, title, status, detail, and instance, then serialize to JSON for consistent error responses.
JsonResponse with structured ApiProblem objects).instance field) and custom extensions (via type/title/detail), making it adaptable to domain-specific needs.throw new \Exception() or response()->json() with new ApiProblem().App\Exceptions\Handler) to standardize all HTTP errors.FailedValidationException → ApiProblem).type/title/detail fields, reducing interoperability. Mitigate via:
about:errors:validation, about:errors:auth).ApiProblem behind a service class for easier swaps.500 Internal Server Error include a detail field with stack traces (security risk)?validation.errors) needed, or does RFC 7807 suffice?ApiProblem objects be logged (e.g., Sentry, Laravel Log) without exposing sensitive details?throw new HttpResponseException() with throw new ApiProblem().Illuminate\Validation\ValidationException to return ApiProblem.App\Middleware\FormatErrors to convert exceptions to ApiProblem.phpunit assertions to validate ApiProblem responses.response()->json() calls in controllers with ApiProblem.// Before
return response()->json(['error' => 'Not found'], 404);
// After
throw new ApiProblem(
url: '/users/1',
title: 'User not found',
status: 404,
detail: 'User with ID 1 does not exist'
);
ValidationException to return structured ApiProblem:
use Abc\ApiProblem;
class CustomValidationException extends ValidationException {
public function render($request) {
return (new ApiProblem(
type: 'about:errors:validation',
title: 'Validation failed',
status: 422,
detail: 'The given data was invalid.',
errors: $this->errors()
))->toJson();
}
}
ApiProblem:
public function handle($request, Throwable $exception) {
return (new ApiProblem(
type: $exception->getType(),
title: $exception->getMessage(),
status: $exception->getCode() ?? 500,
detail: $exception->getFile() . ':' . $exception->getLine()
))->toJson();
}
problem schemas.components:
schemas:
Problem:
$ref: 'https://tools.ietf.org/html/rfc7807'
fruitcake/laravel-cors).ApiProblem usage (e.g., reject PRs with manual response()->json()).ApiProblemFactory).authentication_required) can be pre-defined.type values (e.g., about:errors:rate_limit_exceeded).type/detail).detail may expose sensitive info (mitigate via sanitization).500 responses.type field.ApiProblem instances (e.g., 401 Unauthorized).type: about:errors:database for DB failures).ApiProblem for "quick fixes."JsonResponse wrapper that auto-converts to ApiProblem.type, title, status).respect/validation) to check ApiProblem before sending.detail field leaks sensitive data (e.g., detail: "Database error: SQLSTATE[42000]: Syntax error").detail in production (e.g., Log::error($exception); return ApiProblem::fromException($exception)->withoutSensitiveData()).instance field for technical details (only for admins).ApiProblem usage.type values and when to use them.How can I help you explore Laravel packages today?