Robust Error Handling and Middleware Ordering in devlog-ist/landing

When building web applications, especially those leveraging background jobs and middleware, it's crucial to handle errors gracefully and ensure middleware executes in the correct order. Recent work on the devlog-ist/landing project highlights these principles.

This project focuses on creating a platform for developers to share their insights and experiences. Ensuring a smooth user experience and reliable posting functionality is paramount.

Error Logging in Background Jobs

Background jobs are often used for tasks that don't need to be executed immediately, such as publishing a post to external platforms like DevTo. When these jobs fail, it's essential to have detailed error logs to diagnose the issue. Previously, the error logging in the PublishPostOnDevto job was insufficient, making it difficult to pinpoint the cause of failures.

The fix involved logging the actual error details – the API response, missing keys, or any other exceptions – before re-throwing the error. This ensures that failed jobs are diagnosable, allowing developers to quickly identify and resolve the underlying problems. For example:

try {
    $response = $devtoApi->publish($post);
} catch (\Exception $e) {
    Log::error('Failed to publish post to DevTo', [
        'post_id' => $post->id,
        'error' => $e->getMessage(),
        'response' => $response ?? null, // Ensure $response is defined
    ]);
    throw $e;
}

Middleware Ordering for Subdomain Handling

Middleware plays a vital role in handling requests and ensuring the application's logic is executed correctly. In this project, middleware is used to ensure users are on their own subdomain. However, the order in which middleware is executed can significantly impact the application's behavior.

The EnsureUserOnOwnSubdomain middleware was being executed before the StartSession middleware. This meant that $request->user() was not yet available when the subdomain check was performed. As a result, redirects to the correct subdomain were not firing, leading to errors like "relation posts does not exist" due to an unset search path. Moving EnsureUserOnOwnSubdomain after StartSession in the Filament middleware stack resolved this issue, ensuring that the user is authenticated and the correct subdomain is enforced.

The correct middleware order is visualized below:

graph LR
    A[Request] --> B(StartSession)
    B --> C{User Authed?}
    C -- Yes --> D(EnsureUserOnOwnSubdomain)
    C -- No --> E[Redirect to Login]
    D --> F[Handle Request]

Key Takeaways

  • Detailed error logging in background jobs is crucial for diagnosing and resolving failures.
  • Middleware ordering can significantly impact application behavior. Ensure middleware that depends on user authentication is executed after the session has been started.
  • Always consider the order of operations when implementing new features or modifying existing ones.
Robust Error Handling and Middleware Ordering in devlog-ist/landing
Gerardo Ruiz

Gerardo Ruiz

Author

Share: