Skip to content

feat: handle exceptions globally and add health checks - #30

Merged
RicardoZambon merged 1 commit into
mainfrom
feature/global-exception-handling
Oct 26, 2026
Merged

RicardoZambon merged 1 commit into
mainfrom
feature/global-exception-handling

Conversation

@RicardoZambon

Copy link
Copy Markdown
Owner

Why

Consuming APIs wrap every controller action in the same try/catch ladder. Zilia applications got a global handler and health checks from Zilia.Apps.Core, which other applications cannot use. This brings both into ZWebAPI so the Niten APIs can drop their catch blocks.

What changes

  • Global exception handling. AddGlobalExceptionHandling / UseGlobalExceptionHandling turn any exception that leaves an action into an RFC 7807 response.
    • Default mappings: 404 for EntityNotFoundException<T>, 400 with entityKey for EntityValidationFailureException<TKey>, 403 for MissingUserPermissionException, 500 for anything else.
    • Applications add or override mappings with Map<T>. A mapping matches the exact type, then an open generic, then each base type.
    • Faults go to a new IExceptionReporter the application registers; its error id is written as errorId. Exception detail is written only in Development, and only when enabled.
  • Action arguments breadcrumb. ActionArgumentsBreadcrumbFilter, registered with the handler, sends the action's arguments with the report. This is the context the removed catch blocks used to add.
  • Health checks. AddDefaultHealthChecks / MapDefaultHealthChecks: /health runs no check, /health/ready checks that the database answers.
  • ZWebAPI now references the ASP.NET Core shared framework.

The handler, options and health check are ported from Zilia.Apps.Core, with neutral names and the reporter interface in place of its Zilia-specific service.

Testing

dotnet test passes on net8.0, net9.0 and net10.0 (293 tests), including new tests for the options, the handler, the filter and the health check.

🤖 Generated with Claude Code

Each consuming API wrapped every controller action in the same try/catch
ladder. The handler now turns any exception that leaves an action into an
RFC 7807 response. The defaults map a missing entity to 404, a failed entity
validation to 400 with the entityKey the batch editors read, and a missing
permission to 403. Applications add or override mappings in one place.

The code comes from Zilia.Apps.Core, which only Zilia applications can use.
It reports faults through a new IExceptionReporter instead of a
Zilia-specific service. A breadcrumb filter keeps the action's arguments in
the report, the context the removed catch blocks used to send.

Health checks come with it: /health answers without touching the
database, and /health/ready checks that the database answers.

ZWebAPI now references the ASP.NET Core shared framework.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@RicardoZambon
RicardoZambon merged commit 38cad30 into main Oct 26, 2026
6 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.16.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant