An Innovative Method to Building Online Urdu Corpus (اردو کارپس)
Read More
Error handling is a critical aspect of any web application, and Laravel 12 provides robust tools to manage exceptions gracefully. Proper error handling improves user experience, simplifies debugging, and enhances application security. In this article, we'll explore the best practices for handling errors in Laravel 12 applications.
Laravel comes with excellent exception handling out of the box through the App\Exceptions\Handler class. This class contains two important methods:
public function register(): void
{
$this->reportable(function (Throwable $e) {
// Custom reporting logic
});
}
public function render($request, Throwable $e)
{
// Custom rendering logic
return parent::render($request, $e);
}
Best Practice: Use the register() method to define how different exceptions should be reported and the render() method to customize how exceptions are converted to HTTP responses.
Create domain-specific exceptions for better error categorization.
namespace App\Exceptions;
use Exception;
class PaymentFailedException extends Exception
{
public function __construct($message = 'Payment processing failed')
{
parent::__construct($message);
}
}
Then you can throw and handle them specifically:
$this->reportable(function (PaymentFailedException $e) {
// Special handling for payment failures
if ($this->shouldReport($e)) {
// Notify payment team
}
});
Always return appropriate HTTP status codes:
abort(403, 'Unauthorized action.'); // Forbidden
abort(404, 'Page not found.'); // Not Found
abort(422, 'Validation failed.'); // Unprocessable Entity
abort(500, 'Server error.'); // Internal Server Error
Customize error pages for different HTTP status codes:
Configure proper logging in config/logging.php:
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'slack'],
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => env('LOG_LEVEL', 'critical'),
],
],
Best Practice: Use different log levels appropriately:
For API responses, return validation errors in a consistent format:
public function store(Request $request)
{
$validated = $request->validate([
'email' => 'required|email',
'password' => 'required|min:8',
]);
// ... rest of the logic
}
Laravel will automatically return a 422 response with errors if validation fails.
For custom formatting, override the invalidJson() method in your app/Http/Controllers/Controller.php:
protected function invalidJson($request, ValidationException $exception)
{
return response()->json([
'message' => 'The given data was invalid.',
'errors' => $exception->errors(),
'code' => $exception->status,
], $exception->status);
}
Create middleware for additional exception handling:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class HandleApiErrors
{
public function handle(Request $request, Closure $next)
{
$response = $next($request);
if ($response->status() >= 500) {
// Log server errors with additional context
Log::error('Server error', [
'url' => $request->fullUrl(),
'method' => $request->method(),
'status' => $response->status(),
]);
}
return $response;
}
}
For APIs, maintain consistent error response structures:
return response()->json([
'success' => false,
'message' => 'Resource not found',
'errors' => ['id' => ['The requested resource does not exist']],
'code' => 404
], 404);
Wrap database operations in try-catch blocks:
try {
DB::beginTransaction();
// Database operations
DB::commit();
} catch (\Throwable $e) {
DB::rollBack();
if ($e instanceof QueryException) {
// Handle database query errors
return response()->json(['message' => 'Database error occurred'], 500);
}
throw $e;
}
Integrate professional error tracking:
composer require sentry/sentry-laravel
Configure in .env:
SENTRY_LARAVEL_DSN=https://[email protected]/yourproject
Write tests for your error handling.
public function test_invalid_login_returns_proper_error()
{
$response = $this->postJson('/api/login', [
'email' => '[email protected]',
'password' => 'wrongpassword'
]);
$response->assertStatus(401)
->assertJson([
'success' => false,
'message' => 'Invalid credentials'
]);
}
public function render($request, Throwable $e)
{
if (app()->environment('local')) {
return parent::render($request, $e);
}
// Production error handling
if ($e instanceof ModelNotFoundException) {
return response()->view('errors.404', [], 404);
}
return response()->view('errors.500', [], 500);
}
Effective error handling in Laravel 12 involves leveraging the framework's built-in capabilities while implementing custom solutions for your specific needs. By following these best practices, you'll create applications that:
Remember that error handling is not just about catching exceptions—it's about creating a robust user experience even when things go wrong.
Recent posts form our Blog
0 Comments
Like 0