Skip to content

Replace exception-based error handling with a Result/Error pattern across the API - #113

Merged
therealmoronto merged 18 commits into
mainfrom
exception-to-resul-pattern
Sep 8, 2026
Merged

therealmoronto merged 18 commits into
mainfrom
exception-to-resul-pattern

Conversation

@therealmoronto

Copy link
Copy Markdown
Owner

What

Introduces Result / Result<T> (Nimbo.Wms.Contracts/Result.cs) and an Error value type with
ErrorType (NotFound, Conflict, Validation, BusinessRule, Unexpected), plus a ToActionResult
extension (Nimbo.Wms/Extensions/ResultExtensions.cs) that maps a Result to the right
ProblemDetails status code.

Every command/query handler across the API now returns Result/Result<T> instead of throwing
NotFoundException/ConcurrencyException for expected failure paths (not-found, version
conflicts), and every controller action returns result.ToActionResult(this) instead of raw
DTOs/NoContent()/an unguarded throw.

Scope

Converted end-to-end: MasterData (Items, Suppliers), Topology (Warehouses, Zones, Locations),
Stock (InventoryItems), and Documents — Adjustment, CycleCount, Receiving, Relocation, Shipment
(including lines and Shipment's pick-lines/change-customer endpoints).

Why

Exceptions for expected outcomes (a document that doesn't exist, an optimistic-concurrency
mismatch) made control flow and status-code mapping implicit and scattered across a global
exception middleware. Result makes failure an explicit, typed return value handlers and
controllers both reason about directly, and keeps the HTTP status mapping in one place
(ResultExtensions).

Notes

Fixed two pre-existing copy/paste bugs surfaced while porting Receiving/Relocation's Patch
handlers: the Title branch was calling ChangeCode instead of ChangeTitle.

Testing

dotnet build — 0 errors. dotnet test (unit + integration, Docker/Testcontainers) — all passing.

therealmoronto and others added 17 commits September 7, 2026 16:30
- Introduce `Result` and `Result<T>` to represent success or failure states in operations.
- Define `Error` class with `ErrorType` enumeration for structured error handling.
- Implement extension methods `ToActionResult` and `ToActionResult<T>` for mapping `Result` and `Result<T>` to appropriate HTTP responses.
- Introduce internal `Problem` method for structured error handling based on `ErrorType`.
…ized operation outcomes and update controllers accordingly.
…ardization for API responses

- Update command and query handlers to return `Result` or `Result<T>` types consistently.
- Enhance `ToActionResult` extension with action name support for improved response construction.
- Adjust controllers to leverage `ToActionResult` for standardized and structured API responses.
- Refactor related tests to align with updated response structures.
…om `Value`-based responses to standardized `Result` types

- Update command handlers, models, and tests to return `Result` or `Result<T>` for consistent API response handling.
- Replace `Value` with `Id` in response models (e.g., `CreateItemResponse`, `AddZoneResponse`) and adjust all references/test cases.
- Extend `ToActionResult` to support flexible value selectors and improve response generation.
- Standardize error handling across operations with the `Result`-based approach, modifying controllers to leverage updated handlers.
- Refactor tests to align with the new `Result` pattern and simplified response models.
…tandardized response handling and update controllers with `ToActionResult` integration
…tandardized response handling and update controllers with `ToActionResult` integration
…onsistent response standardization and integrate `ToActionResult` in controllers.
…onsistent response standardization and integrate `ToActionResult` in controllers.
… for standardized response handling and integrate `ToActionResult` in controllers.
… exception-to-resul-pattern

# Conflicts:
#	Nimbo.Wms/Controllers/Documents/AdjustmentDocumentsController.cs
… Result pattern

Mirrors the Adjustment/CycleCount conversion already on this branch:
- Commands/queries return Result / Result<T> instead of throwing NotFoundException/ConcurrencyException.
- Handlers return Error.NotFound/Error.Conflict instead of throwing.
- Controllers use ToActionResult(this) instead of raw DTOs/NoContent, matching the ProblemDetails contract.

Covers ReceivingDocument(Lines), RelocationDocument(Lines), ShipmentDocument(Lines),
ShipmentPickLines, and ChangeCustomer for full bounded-context consistency.

Also fixed a pre-existing copy/paste bug in Receiving/Relocation Patch handlers where
the Title branch called ChangeCode instead of ChangeTitle.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K3RVvP61dDJgVjGcA4H5oS
…ndlers and controllers to adopt `Result` pattern for consistent error handling and response generation.
…rn for consistent error handling and standardized response generation.
@therealmoronto
therealmoronto merged commit d426f98 into main Sep 8, 2026
6 checks passed
@therealmoronto
therealmoronto deleted the exception-to-resul-pattern branch September 8, 2026 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant