From 060e0c5652de52f9c003a030977598d9bcabd591 Mon Sep 17 00:00:00 2001 From: Will Date: Thu, 22 Oct 2026 08:58:03 -0400 Subject: [PATCH] Add correlationId to the 400 for a missing customerId --- .../northstar/crm/api/ApiExceptionHandler.java | 12 +++++++++--- .../crm/api/InteractionControllerTest.java | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/com/northstar/crm/api/ApiExceptionHandler.java b/backend/src/main/java/com/northstar/crm/api/ApiExceptionHandler.java index 216ae33..e7cedcc 100644 --- a/backend/src/main/java/com/northstar/crm/api/ApiExceptionHandler.java +++ b/backend/src/main/java/com/northstar/crm/api/ApiExceptionHandler.java @@ -83,14 +83,20 @@ protected ResponseEntity handleMethodArgumentNotValid( .body(problem); } - /** Framework MVC errors (missing param, unreadable body, ...) also carry the correlation id. */ + /** + * Framework MVC errors (missing param, unreadable body, ...) also carry the correlation id. + * Some arrive with a null body (a missing query parameter, for one) and the superclass builds + * the ProblemDetail itself, so the id is added to the body it returns. + */ @Override protected ResponseEntity handleExceptionInternal( Exception ex, Object body, HttpHeaders headers, HttpStatusCode statusCode, WebRequest request) { - if (body instanceof ProblemDetail problem) { + ResponseEntity response = + super.handleExceptionInternal(ex, body, headers, statusCode, request); + if (response != null && response.getBody() instanceof ProblemDetail problem) { withCorrelation(problem); } - return super.handleExceptionInternal(ex, body, headers, statusCode, request); + return response; } static ProblemDetail withCorrelation(ProblemDetail problem) { diff --git a/backend/src/test/java/com/northstar/crm/api/InteractionControllerTest.java b/backend/src/test/java/com/northstar/crm/api/InteractionControllerTest.java index 1f7c98f..0f61b55 100644 --- a/backend/src/test/java/com/northstar/crm/api/InteractionControllerTest.java +++ b/backend/src/test/java/com/northstar/crm/api/InteractionControllerTest.java @@ -272,4 +272,20 @@ void create_withoutBearer_returns401() throws Exception { verify(interactionService, never()).create(any(), any()); } + + /** Missing customerId is a framework 400; it carries the request correlation id like the rest. */ + @Test + void list_missingCustomerId_shouldCarryTheRequestCorrelationId() throws Exception { + mockMvc + .perform( + get("/api/v1/interactions") + .header("Authorization", bearer) + .header("X-Correlation-Id", "missing-param-001")) + .andExpect(status().isBadRequest()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_PROBLEM_JSON)) + .andExpect(jsonPath("$.status").value(400)) + .andExpect(jsonPath("$.correlationId").value("missing-param-001")); + + verify(interactionService, never()).list(any()); + } }