Skip to content
Muhammet Şafak
tr
Answered:

How should I design the exception hierarchy in a library so consumers can catch cleanly?


Question

I'm authoring an SDK and I'm stuck on exception design. I want consumers to be able to catch a single thing (a marker interface) and say "this error came from my library". But at the same time I want the caller to distinguish a recoverable error (a transient network issue, retryable) from a fatal one (misuse, a config error). How do I combine these two needs in one hierarchy in PHP? Nothing more, nothing less.

Answer

Short answer

Define a marker interface at the root of your library and have every exception you throw implement it; then signal recoverable vs fatal with SPL base classes and, where needed, a thin sub-marker.

Context

PHP’s interfaces are exactly what let you combine both in one class.

Why

  1. Let the root marker extend \Throwable. Declare interface AcmeException extends \Throwable {}. Every exception you throw implements it, so the caller can write catch (AcmeException $e) in one line and say “this is from my SDK”. This is the approach the PSRs adopted too.

  2. Have concrete classes extend the right SPL base. That way they carry both your marker and the standard hierarchy. LogicException is a programmer error (misuse, config — fatal, don’t retry); RuntimeException is a runtime/environmental error (network, timeout — often recoverable). That split already gives you half the recoverable/fatal signal.

What to do

  1. Mark recoverable with a separate thin marker. Rather than burying “retryable” in a base class, turn it into an explicit contract: interface RetryableException extends AcmeException {}. The caller catches only that, backs off and retries; it treats everything else as fatal.

    interface AcmeException extends \Throwable {}
    interface RetryableException extends AcmeException {}
    
    // Transient — the caller may retry
    final class ConnectionException extends \RuntimeException implements RetryableException {}
    
    // Permanent — misuse/config, retrying is pointless
    final class InvalidConfigException extends \InvalidArgumentException implements AcmeException {}
  2. Never throw a bare \Exception or \RuntimeException. Everything leaving your library must carry the marker, or the caller’s single-catch promise is broken. When you catch a lower layer’s exceptions (Guzzle, PDO) and wrap them in your own type, preserve the chain with $previous so diagnostics aren’t lost.

  3. Carry context as typed properties/getters. Don’t make callers parse the message; expose getters like ->getStatusCode(), ->getRetryAfter(). When the caller decides what to do, let it look at the type and the data, not a string.

  4. Don’t over-engineer. Start with the marker + Retryable + a few concrete types. Add classes as the need appears; because you expose the contract through interfaces, splitting concrete classes later won’t break callers.

Bottom line: personally I’d build three layers: a root AcmeException (marker), a RetryableException (the recoverable contract), and concrete classes extending the correct SPL base (Logic vs Runtime). The caller can catch broadly (AcmeException) or catch only the recoverable ones and retry. That satisfies both your “single marker” wish and the recoverable/fatal distinction without adding overhead.

Compiled from questions I have run into in the field or been asked.

Related Reading

Comments

Sign in with your GitHub account to join the discussion. Comments are stored in GitHub Discussions.

More Questions

All questions

Search the site

Start typing to search posts, projects and pages.

Esc to close Powered by Pagefind