Skip to content

Two overlapping, inconsistent global exception-handling middlewares #39

Description

@umerfaruk

Summary

`ErrorHandlerMiddleware` and `StatusCodeMiddleware` both catch exceptions globally, but are nested such that one is effectively dead code, and their response shapes differ.

Details

Pipeline order in `Program.cs:221-226`: `ErrorHandlerMiddleware` wraps `LibraryConfigurationMiddleware` which wraps `StatusCodeMiddleware` which wraps the controllers. `StatusCodeMiddleware`'s catch-all (`catch (Exception ex)`) swallows everything from the controller layer down without rethrowing, so `ErrorHandlerMiddleware` only ever sees exceptions thrown in `LibraryConfigurationMiddleware` itself (or above it).

Response shape also differs between the two:

  • `ErrorHandlerMiddleware`: JSON body `{ "message": ... }`
  • `StatusCodeMiddleware`: empty body for most statuses, plain text for `BadRequestException`

Impact

Confusing/inconsistent error responses depending on exactly where an exception originates, and one middleware is largely redundant.

Suggested fix

Consolidate into a single exception-handling middleware with one consistent response contract — ideally `application/problem+json` (see #15). Also relevant to fixing #6 (exception message leakage) cleanly.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions