Skip to content

Feat/global exception handling#17

Open
9Queens wants to merge 22 commits into
Accepted-GR:mainfrom
9Queens:feat/global-exception-handling
Open

Feat/global exception handling#17
9Queens wants to merge 22 commits into
Accepted-GR:mainfrom
9Queens:feat/global-exception-handling

Conversation

@9Queens

@9Queens 9Queens commented Jul 7, 2026

Copy link
Copy Markdown

Added new Middleware tests and a new postman collection that will directly call our dockerised endpoints

  • postman-local.json file added inside 'src' folder to be used when container is up and running
  • hardcoded "localhost:8080" is used for the new postman collection

Konstantinos Georgomitros added 22 commits July 5, 2026 13:09
…I config

- Replace per-instance HttpClient with typed HttpClient via IHttpClientFactory
- Remove BuildServiceProvider anti-pattern; accept IConfiguration in DefaultConfiguration
- Inject HttpClient into ProductsService and make JSON deserialization null-safe with logging
- Pass builder.Configuration into registration calls in Program.cs
- Add GetProductById/CreateProduct to IProductsService and ProductsService, exttending API endpoints, and include unit tests
- Test project added + ProductService tests added
- Add TokenService, AuthenticationDelegatingHandler, DI wiring and memory cache
- Attach Bearer token to typed HttpClients and add unit tests (token + handler)
- add PerformanceLoggingMiddleware Measure incoming request duration and log warnings when requests exceed configured threshold; support excluded paths.
- add HttpClientMetricsHandler Measure outgoing HttpClient call durations and emit structured metrics/warnings based on performance settings.
- register metrics handler and middleware Register HttpClientMetricsHandler and AuthenticationDelegatingHandler with typed HttpClients and add PerformanceLoggingMiddleware to pipeline.
- add unit tests for performance logging and metrics Add tests for slow incoming requests and slow outgoing HttpClient calls; add TestLogger helpers and console output for visibility.
 - move validators/mappers to Mapping/Validation
 - add upstream category mapper
 - DI helpers, injectable API validators
 - more unit tests.
- Multi-stage Dockerfile for optimized builds
- Docker Compose for dev/prod/test environments
- Fix TokenService HttpClient registration and auth payload
- Update API configuration for correct upstream paths
- Add Docker documentation
- GETTING_STARTED.md file added for the readers to start operating using docker
…oss the solution using MediatR library to improve separation of concerns and maintainability.

Architecture Changes:
- Replace service-based approach with CQRS handlers
- Introduce IMediator for centralized request handling
- Separate read operations (Queries) from write operations (Commands)

Added:
- MediatR package (v14.2.0) to Application layer
- Query handlers for Products (GetProducts, GetProductById)
- Query handlers for Categories (GetCategories, GetCategoryById)
- Command handlers for Products (CreateProduct)
- Command handlers for Categories (CreateCategory)
- Moved endpoint registration from Program.cs to feature-specific files:
  - ProductEndpoints.cs for product routes
  - CategoryEndpoints.cs for category routes
  - EndpointExtensions.cs with single MapVersionedEndpoints() entry point

- Updated tests to verify CQRS handlers instead of legacy services:
  - ProductsServiceTests → ProductQueryHandlerTests + ProductCommandHandlerTests
  - CategoriesServiceTests → CategoryQueryHandlerTests + CategoryCommandHandlerTests

- Removed legacy service layer (no longer used with CQRS):
  - Deleted ProductsService.cs, CategoriesService.cs
  - Deleted IProductsService.cs, ICategoriesService.cs
…nt management

- Updated all query/command handlers to inject IHttpClientFactory instead of HttpClient
- Configured named HttpClient "RestApiClient" with BaseAddress, timeout, and message handlers
- Added Microsoft.Extensions.Http package to Application project
- Updated unit tests to mock IHttpClientFactory with Moq
- Simplified Infrastructure DI registration by removing explicit handler overrides
- Fixed Docker runtime InvalidOperationException: BaseAddress must be set
…nnotations

- Removed obsolete 'version' key from docker-compose.yml and docker-compose.override.yml
- Renamed test files to match CQRS pattern: *ServiceTests → *HandlerTests
- Enabled nullable reference types in test project for consistency
- Add ValidationBehavior pipeline for centralized validation
- Create FluentValidation validators for Product and Category commands
- Remove duplicate validation logic from API endpoints
- Simplify endpoint code to rely on pipeline validation
- Add comprehensive validator unit tests (10 new tests)
- Update DI to register validators and pipeline behavior
 - unused usings
 - removing obsolete API validations
 - removing unsused parameters
Add centralized exception handling for cleaner, more maintainable code:

- Create ErrorResponse model with statusCode, message, errors, traceId, timestamp
- Implement GlobalExceptionHandlerMiddleware:
   - Catches FluentValidation.ValidationException → 400 with detailed errors
   - Catches common exceptions → appropriate HTTP status codes
   - Logs all exceptions with TraceId for debugging
   - Returns consistent JSON error responses

Also:
- Remove try-catch blocks from ProductEndpoints and CategoryEndpoints
- Register middleware early in pipeline (before PerformanceLoggingMiddleware)
… call our dockerized endpoints

 - postman-local.json file added to 'src' folder for imediate copy and paste to postman desktop with hardcoded "localhost:8080"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant