$ recombobulate _
home / tips / use-claude-to-write-centralised-api-error-handling-middleware
163

Use Claude to Write Centralised API Error Handling Middleware

recombobulate @recombobulate · Mar 25, 2026 · Workflows
use-claude-to-write-centralised-api-error-handling-middleware

Inconsistent API error responses — different shapes for validation errors, auth failures, and server errors — make every frontend integration harder than it needs to be. Let Claude write centralised middleware to enforce a single contract.

Ask Claude: "Write Express middleware that catches all errors and returns consistent
JSON responses. Handle validation errors, auth failures, not-found errors, and
unexpected exceptions. Log unexpected errors with context, but never leak stack
traces to clients."

Claude generates middleware covering the full error surface — Zod/Joi validation failures, JWT auth errors, database constraint violations, and unhandled exceptions — all formatted to the same response shape:

// Every error returns this shape
{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "The request data is invalid.",
    "details": [
      { "field": "email", "message": "Must be a valid email address" }
    ]
  }
}

For Laravel, ask Claude to update your app/Exceptions/Handler.php render method to normalise all exceptions — including third-party ones — into your API's contract with appropriate HTTP status codes:

// Claude maps exception types to consistent JSON shapes
public function render($request, Throwable $e): Response
{
    if ($request->expectsJson()) {
        return $this->renderApiException($e);
    }
    return parent::render($request, $e);
}

Claude will also write tests that assert each error type returns the correct HTTP status and shape — so the contract doesn't drift over time.

Consistent error responses make every API integration easier — and they're the mark of a backend built with care.

~/recombobulate $ tip --comments --count=0

Log in to leave a comment.

~/recombobulate $ tip --related --limit=3
0
Run Claude Code in GitHub Actions to Automatically Review Every Pull Request

Set up Claude Code as an automated reviewer in your CI pipeline — on every pull request, it reads the diff, checks for bugs, security issues, missing tests, and convention violations, then posts its findings as a PR comment. Your human reviewers get a head start because the obvious issues are already flagged before they look.

recombobulate @recombobulate · 1 month ago
0
Ask Claude to Build a Deployment Checklist from Your Actual Infrastructure

Before deploying, tell Claude to read your project — migrations, environment variables, queue workers, scheduled tasks, caching, third-party integrations — and generate a deployment checklist that's specific to your app. Not a generic "did you run migrations?" list, but one that knows YOUR infrastructure and catches the things YOUR deploy can break.

recombobulate @recombobulate · 1 month ago
0
Ask Claude to Generate a README from Your Actual Codebase — Not a Template

Instead of writing a README from memory or copying a template, tell Claude to read your project and generate one that's actually accurate — real setup instructions from your config, real architecture from your directory structure, real API examples from your routes, and real prerequisites from your dependency files.

recombobulate @recombobulate · 1 month ago