bugloos/error-response-bundle
Installation:
composer require bugloos/error-response-bundle
Register the bundle in config/bundles.php (Symfony-only):
return [
// ...
Bugloos\ErrorResponseBundle\BugloosErrorResponseBundle::class => ['all' => true],
];
Basic Usage: Trigger an error response in a controller:
use Bugloos\ErrorResponseBundle\Exception\ErrorResponseException;
public function exampleAction()
{
throw new ErrorResponseException('Something went wrong', 400);
}
The response will automatically format as JSON with a standardized structure:
{
"success": false,
"message": "Something went wrong",
"errors": [],
"data": null,
"status": 400
}
First Use Case:
Replace generic JsonResponse or HttpException in APIs for consistent error formatting. Example:
try {
$user = User::findOrFail($id);
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
throw new ErrorResponseException('User not found', 404);
}
Workflow:
Centralized Error Handling:
Override app/Exceptions/Handler.php to convert exceptions to ErrorResponseException:
public function render($request, Throwable $exception)
{
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException) {
return new ErrorResponseException($exception->getMessage(), $exception->getStatusCode());
}
return parent::render($request, $exception);
}
Structured Error Data:
Pass arrays for errors or data fields:
throw new ErrorResponseException('Validation failed', 422, [
'errors' => ['email' => ['The email is invalid.']],
'data' => ['input' => $request->all()]
]);
Validation Errors: Integrate with Laravel’s validator:
$validator = Validator::make($request->all(), ['email' => 'required|email']);
if ($validator->fails()) {
throw new ErrorResponseException('Validation failed', 422, [
'errors' => $validator->errors()->toArray()
]);
}
Patterns:
ErrorResponseException for custom responses:
throw new ErrorResponseException('Unauthorized', 401, [], [
'auth' => ['token' => 'required']
]);
public function handle($request, Closure $next)
{
try {
return $next($request);
} catch (ErrorResponseException $e) {
return response()->json($e->getResponseData(), $e->getStatusCode());
}
}
Mock Responses:
$exception = new ErrorResponseException('Test error', 500);
$this->expectException(\Bugloos\ErrorResponseBundle\Exception\ErrorResponseException::class);
$this->expectExceptionMessage('Test error');
Symfony vs. Laravel:
app/Exceptions/Handler.php or use middleware.render() as shown above.Response Overrides:
success/status fields in your app.->merge() to combine custom data:
$response = $exception->getResponseData();
$response['meta'] = ['timestamp' => now()];
Validation Edge Cases:
'errors' => array_merge(...$validator->errors()->toArray())
Missing Responses:
ErrorResponseException or its handler. Check Symfony’s ProblemDetails middleware if using Symfony components.Status Code Conflicts:
HttpException may override status codes. Explicitly set:
throw new ErrorResponseException('Error', 500, [], [], 500);
Custom Response Classes:
Extend ErrorResponseException for domain-specific errors:
class PaymentErrorException extends ErrorResponseException {
public function __construct(string $message, array $paymentData = []) {
parent::__construct($message, 402, [], ['payment' => $paymentData]);
}
}
Dynamic Fields: Add computed fields to responses:
$exception->setResponseData('debug', app()->environment('local'));
Localization:
Override getMessage() to support translations:
throw new ErrorResponseException(__('errors.user.not_found'), 404);
ProblemDetails, disable it to avoid conflicts:
# config/packages/framework.yaml
framework:
http_client:
problem_details: false
How can I help you explore Laravel packages today?