fix(mobile-api): event errors use the portal's details/errors contract
The portal forwards only code, errors and flat details from a ProblemDetail, so the failing field and the TEAM_NOT_SAVED event uuid move into details and the missing-field list into errors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
c908fbe9fc
commit
2efdcba1ae
2 files changed
+17
-6
No files matched your search
+4
-4
@@ -135,7 +135,7 @@ public class AdminEventService {
|
|||||||
List<String> missing = in.entrySet().stream().filter(e -> !StringUtils.hasText(e.getValue())).map(Map.Entry::getKey).toList();
|
List<String> missing = in.entrySet().stream().filter(e -> !StringUtils.hasText(e.getValue())).map(Map.Entry::getKey).toList();
|
||||||
if (!missing.isEmpty()) {
|
if (!missing.isEmpty()) {
|
||||||
CodedResponseStatusException e = error(HttpStatus.BAD_REQUEST, "Required: " + String.join(", ", missing), "REQUIRED_FIELD", missing.get(0));
|
CodedResponseStatusException e = error(HttpStatus.BAD_REQUEST, "Required: " + String.join(", ", missing), "REQUIRED_FIELD", missing.get(0));
|
||||||
e.getBody().setProperty("fields", missing);
|
e.getBody().setProperty("errors", missing);
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
String name = r.name().trim();
|
String name = r.name().trim();
|
||||||
@@ -200,7 +200,7 @@ public class AdminEventService {
|
|||||||
} catch (RuntimeException ex) {
|
} catch (RuntimeException ex) {
|
||||||
CodedResponseStatusException e = error(HttpStatus.BAD_GATEWAY,
|
CodedResponseStatusException e = error(HttpStatus.BAD_GATEWAY,
|
||||||
"Event " + eventUuid + " was saved but its team was not; save the team again", "TEAM_NOT_SAVED", null);
|
"Event " + eventUuid + " was saved but its team was not; save the team again", "TEAM_NOT_SAVED", null);
|
||||||
e.getBody().setProperty("eventUuid", String.valueOf(eventUuid));
|
e.getBody().setProperty("details", Map.of("eventUuid", String.valueOf(eventUuid)));
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -248,10 +248,10 @@ public class AdminEventService {
|
|||||||
return error(HttpStatus.NOT_FOUND, "Event not found", "EVENT_NOT_FOUND", null);
|
return error(HttpStatus.NOT_FOUND, "Event not found", "EVENT_NOT_FOUND", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** field, when given, rides on the ProblemDetail body next to code, so the portal can mark the failing input. */
|
/** field, when given, rides in the ProblemDetail's details (the portal forwards code, errors and flat details only). */
|
||||||
private static CodedResponseStatusException error(HttpStatus status, String reason, String code, String field) {
|
private static CodedResponseStatusException error(HttpStatus status, String reason, String code, String field) {
|
||||||
CodedResponseStatusException e = new CodedResponseStatusException(status, reason, code);
|
CodedResponseStatusException e = new CodedResponseStatusException(status, reason, code);
|
||||||
if (field != null) e.getBody().setProperty("field", field);
|
if (field != null) e.getBody().setProperty("details", Map.of("field", field));
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+13
-2
@@ -107,10 +107,21 @@ class AdminEventServiceTest {
|
|||||||
assertThatThrownBy(r::run).isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
assertThatThrownBy(r::run).isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
||||||
assertThat(e.getStatusCode()).isEqualTo(status);
|
assertThat(e.getStatusCode()).isEqualTo(status);
|
||||||
assertThat(e.getCode()).isEqualTo(code);
|
assertThat(e.getCode()).isEqualTo(code);
|
||||||
if (field != null) assertThat(e.getBody().getProperties()).containsEntry("field", field);
|
// the portal forwards only code, errors (string list) and details (flat object) from a ProblemDetail
|
||||||
|
if (field != null) assertThat((Map<String, Object>) e.getBody().getProperties().get("details")).containsEntry("field", field);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void create_allMissingFieldsAreListedInErrors() {
|
||||||
|
assertThatThrownBy(() -> service.create(req("N", " ", null, "", null)))
|
||||||
|
.isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
||||||
|
assertThat(e.getCode()).isEqualTo("REQUIRED_FIELD");
|
||||||
|
assertThat(e.getBody().getProperties().get("errors")).isEqualTo(List.of("location", "startDate", "endDate"));
|
||||||
|
assertThat((Map<String, Object>) e.getBody().getProperties().get("details")).containsEntry("field", "location");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private void assertRefusedWithoutSave(AdminEventRequest r, String code, String field) {
|
private void assertRefusedWithoutSave(AdminEventRequest r, String code, String field) {
|
||||||
assertCoded(() -> service.create(r), HttpStatus.BAD_REQUEST, code, field);
|
assertCoded(() -> service.create(r), HttpStatus.BAD_REQUEST, code, field);
|
||||||
verify(rest, never()).postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any());
|
verify(rest, never()).postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any());
|
||||||
@@ -309,7 +320,7 @@ class AdminEventServiceTest {
|
|||||||
.isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
.isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
||||||
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.BAD_GATEWAY);
|
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.BAD_GATEWAY);
|
||||||
assertThat(e.getCode()).isEqualTo("TEAM_NOT_SAVED");
|
assertThat(e.getCode()).isEqualTo("TEAM_NOT_SAVED");
|
||||||
Object uuid = e.getBody().getProperties().get("eventUuid");
|
Object uuid = ((Map<String, Object>) e.getBody().getProperties().get("details")).get("eventUuid");
|
||||||
assertThat(uuid).isInstanceOf(String.class);
|
assertThat(uuid).isInstanceOf(String.class);
|
||||||
assertThat(e.getReason()).contains((String) uuid);
|
assertThat(e.getReason()).contains((String) uuid);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user