Skip to content

Commit

Permalink
Add KiwiStandardResponses method to create a "standard" 500 error
Browse files Browse the repository at this point in the history
* Add standardInternalServerErrorResponse
* Add standardInternalServerErrorResponseBuilder

Closes  #1164
  • Loading branch information
sleberknight committed Aug 9, 2024
1 parent 8edbc5c commit da506ca
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/java/org/kiwiproject/jaxrs/KiwiStandardResponses.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,28 @@ public static Response.ResponseBuilder standardNotFoundResponseBuilder(String er
return standardErrorResponseBuilder(Response.Status.NOT_FOUND, errorDetails);
}

/**
* Returns a 500 Internal Server Error response containing an {@link ErrorMessage} entity which uses
* {@code errorDetails} as the detailed error message.
*
* @param errorDetails the error message to use
* @return a 500 Internal Server Error response with {@code application/json} content type
*/
public static Response standardInternalServerErrorResponse(String errorDetails) {
return standardInternalServerErrorResponseBuilder(errorDetails).build();
}

/**
* Returns a response builder with 500 Internal Server Error status and an {@link ErrorMessage} entity
* which uses {@code errorDetails} as the detailed error message.
*
* @param errorDetails the error message to use
* @return a response builder with the given status code and {@code application/json} content type
*/
public static Response.ResponseBuilder standardInternalServerErrorResponseBuilder(String errorDetails) {
return standardErrorResponseBuilder(Response.Status.INTERNAL_SERVER_ERROR, errorDetails);
}

/**
* Returns a response having the given status and an {@link ErrorMessage} entity which uses {@code errorDetails}
* as the detailed error message.
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/org/kiwiproject/jaxrs/KiwiStandardResponsesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,27 @@ void shouldReturnNotFoundResponse_WithErrorMessageEntity() {
}
}

@Nested
class StandardInternalServerErrorResponse {

@Test
void shouldReturnResponse_With500StatusAndErrorMessageEntity() {
var response = KiwiStandardResponses.standardInternalServerErrorResponse("This is the error message. It is very helpful.");

assertResponseEntityHasOneErrorMessage(response, 500, "This is the error message. It is very helpful.");
assertJsonResponseType(response);
}

@Test
void shouldReturnResponseBuilder_With500StatusAndErrorMessageEntity() {
var builder = KiwiStandardResponses.standardInternalServerErrorResponseBuilder("This is the error message. It is very helpful.");
var response = builder.build();

assertResponseEntityHasOneErrorMessage(response, 500, "This is the error message. It is very helpful.");
assertJsonResponseType(response);
}
}

@Nested
class StandardErrorResponse {

Expand Down

0 comments on commit da506ca

Please sign in to comment.