ErrorResponse extends JsonResponse { static String asErrorJson(String message) { try { return objectMapper.writeValueAsString(Map.of("error", message)); } catch (JsonProcessingException e) { log.log(WARNING, "Could not encode error message to json:", e); return "Could not encode error message to json, check the log for details."; } } ErrorResponse(int code, String message);   }
@Test public void error_message_is_wrapped_in_json_object() { var json = ErrorResponse.asErrorJson("bad"); assertEquals("{\"error\":\"bad\"}", json); }
@Test public void quotes_are_escaped() { var json = ErrorResponse.asErrorJson("Message \" with \" embedded quotes."); assertEquals("{\"error\":\"Message \\\" with \\\" embedded quotes.\"}", json); }