Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add KiwiStandardResponses method to create a "standard" 500 error #1182

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 a 500 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