PHP HTTP Error Handling Solutions

Generic PHP exception handling using trigger_error() and error_log() is not enough for modern web applications. Proper error handling is not just about logging. It must deliver consistent, user-friendly responses, enforce correct HTTP status codes, and capture structured information:

  • When an error occurs, users see a clear, friendly error message which preserves the site's branding and navigation, instead of a blank page or a generic Apache/Nginx error.
  • Correct HTTP status codes are returned for clients, crawlers, and SEO purposes.
  • Structured error data is captured for debugging, monitoring, and analysis.

My Approach

I design systems that handle exceptions consistently across your application:

Common HTTP errors have dedicated classes, for example:

class PageNotFoundError extends NotFoundError
{
    public function __construct(
        string $title = 'Page Not Found',
        string $detail = 'The requested page could not be found.',
        ?string $message = null,
        ?Throwable $previous = null,
    ) {
        parent::__construct($title, $detail, $message, $previous);
    }
}

These classes are easy to extend and customize. For example, you can have PageNotFoundError, FileNotFoundError, ProductNotFoundError, all special cases of HTTP 404 with different error pages and messages.

Instead of the generic trigger_error() or die(), errors are triggered in a simple, consistent way:

throw new PageNotFoundError();

Central exception handler determines which error page to render, depending on error class.

Error pages preserve the site's layout, brading, and navigation. User knows where to go next.

System enforces and validates HTTP response codes. Client errors must have 4xx, server errors 5xx.

Errors are logged using a dependency injected (therefore swappable, customizable) logging engine and format. See error logging solutions.

What I Offer

Consulting

I can help you decide what needs to get done for consistent, user friendly error handling in your project and how to structure it. I have seen the strengths and weaknesses of alternative solutions, so I can help you save development time, costs, and problems later.

Scope and depth of the consultation is up to you, from a short 30 minutes' talk (which is always a good, low-risk starting point) to multiple work days collaborating with your team.

Partial or Full Implementation

I can implement a complete error handling system or parts of it. For example:

  • Provide full library with individual HTTP error classes. HTTP is standardized, so most likely you can reuse the library I already have (and save costs), perhaps with a few adjustments to error message wording or a few extra custom error classes for your case.
  • Implement exception handler (central point of the error handling system) and integrate it into your project's request handling flow in a clear, easy to maintain way.
  • Design an error logging solution or adapt and integrate your own.

Why Work With Me

  • 15+ years PHP experience and following best practices.
  • I operate live production sites using the same systems and concepts. I know what matters and what it takes to maintain it.
  • User experience first: clear, branded error messages instead of blank pages or generic server errors. See example 404 error page).

If you need help with consistent error handling for your project, from short consultation to full system implementation, contact me.