Skip to content

Commit

Permalink
fix: handling error URL not existent
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioT90 committed Feb 7, 2025
1 parent cd686b9 commit adac2dc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
5 changes: 3 additions & 2 deletions openapi/p4pa-pu-sil.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ components:
code:
type: string
enum:
- PU_SIL_BAD_REQUEST
- PU_SIL_GENERIC_ERROR
- NOT_FOUND
- BAD_REQUEST
- GENERIC_ERROR
message:
type: string
securitySchemes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.web.ErrorResponse;
import org.springframework.web.ErrorResponseException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import java.util.stream.Collectors;

Expand All @@ -25,18 +27,20 @@
@Order(Ordered.HIGHEST_PRECEDENCE)
public class PuSilExceptionHandler {

@ExceptionHandler({ValidationException.class, HttpMessageNotReadableException.class, MethodArgumentNotValidException.class})
@ExceptionHandler({ValidationException.class, HttpMessageNotReadableException.class, MethodArgumentNotValidException.class, MethodArgumentTypeMismatchException.class})
public ResponseEntity<PuSilErrorDTO> handleViolationException(Exception ex, HttpServletRequest request) {
return handleException(ex, request, HttpStatus.BAD_REQUEST, PuSilErrorDTO.CodeEnum.BAD_REQUEST);
}

@ExceptionHandler({ServletException.class})
public ResponseEntity<PuSilErrorDTO> handleServletException(ServletException ex, HttpServletRequest request) {
@ExceptionHandler({ServletException.class, ErrorResponseException.class})
public ResponseEntity<PuSilErrorDTO> handleServletException(Exception ex, HttpServletRequest request) {
HttpStatusCode httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
PuSilErrorDTO.CodeEnum errorCode = PuSilErrorDTO.CodeEnum.GENERIC_ERROR;
if (ex instanceof ErrorResponse errorResponse) {
httpStatus = errorResponse.getStatusCode();
if (httpStatus.is4xxClientError()) {
if(httpStatus.isSameCodeAs(HttpStatus.NOT_FOUND)) {
errorCode = PuSilErrorDTO.CodeEnum.NOT_FOUND;
} else if (httpStatus.is4xxClientError()) {
errorCode = PuSilErrorDTO.CodeEnum.BAD_REQUEST;
}
}
Expand Down Expand Up @@ -64,6 +68,9 @@ private static void logException(Exception ex, HttpServletRequest request, HttpS
getRequestDetails(request),
httpStatus.value(),
ex.getMessage());
if(log.isDebugEnabled() && ex.getCause()!=null){
log.debug("CausedBy: ", ex.getCause());
}
}

private static String buildReturnedMessage(Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void handleMissingServletRequestParameterException() throws Exception {

performRequest(null, MediaType.APPLICATION_JSON)
.andExpect(MockMvcResultMatchers.status().isBadRequest())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("PU_SIL_BAD_REQUEST"))
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("BAD_REQUEST"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Required request parameter 'data' for method parameter type String is not present"));

}
Expand All @@ -114,7 +114,7 @@ void handleRuntimeExceptionError() throws Exception {

performRequest(DATA, MediaType.APPLICATION_JSON)
.andExpect(MockMvcResultMatchers.status().isInternalServerError())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("PU_SIL_GENERIC_ERROR"))
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("GENERIC_ERROR"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Error"));
}

Expand All @@ -125,23 +125,31 @@ void handleGenericServletException() throws Exception {

performRequest(DATA, MediaType.APPLICATION_JSON)
.andExpect(MockMvcResultMatchers.status().isInternalServerError())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("PU_SIL_GENERIC_ERROR"))
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("GENERIC_ERROR"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Error"));
}

@Test
void handle4xxHttpServletException() throws Exception {
performRequest(DATA, MediaType.parseMediaType("application/hal+json"))
.andExpect(MockMvcResultMatchers.status().isNotAcceptable())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("PU_SIL_BAD_REQUEST"))
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("BAD_REQUEST"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("No acceptable representation"));
}

@Test
void handleUrlNotFound() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/NOTEXISTENTURL"))
.andExpect(MockMvcResultMatchers.status().isNotFound())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("NOT_FOUND"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("No static resource NOTEXISTENTURL."));
}

@Test
void handleNoBodyException() throws Exception {
performRequest(DATA, MediaType.APPLICATION_JSON, null)
.andExpect(MockMvcResultMatchers.status().isBadRequest())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("PU_SIL_BAD_REQUEST"))
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("BAD_REQUEST"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Required request body is missing"));
}

Expand All @@ -150,7 +158,7 @@ void handleInvalidBodyException() throws Exception {
performRequest(DATA, MediaType.APPLICATION_JSON,
"{\"notRequiredField\":\"notRequired\",\"lowerCaseAlphabeticField\":\"ABC\"}")
.andExpect(MockMvcResultMatchers.status().isBadRequest())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("PU_SIL_BAD_REQUEST"))
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("BAD_REQUEST"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Invalid request content: lowerCaseAlphabeticField: must match \"[a-z]+\"; requiredField: must not be null"));
}

Expand All @@ -159,7 +167,7 @@ void handleNotParsableBodyException() throws Exception {
performRequest(DATA, MediaType.APPLICATION_JSON,
"{\"notRequiredField\":\"notRequired\",\"dateTimeField\":\"2025-02-05\"}")
.andExpect(MockMvcResultMatchers.status().isBadRequest())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("PU_SIL_BAD_REQUEST"))
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("BAD_REQUEST"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Cannot parse body: dateTimeField: Text '2025-02-05' could not be parsed at index 10"));
}

Expand All @@ -170,7 +178,7 @@ void handle5xxHttpServletException() throws Exception {

performRequest(DATA, MediaType.APPLICATION_JSON)
.andExpect(MockMvcResultMatchers.status().isInternalServerError())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("PU_SIL_GENERIC_ERROR"))
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("GENERIC_ERROR"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("500 INTERNAL_SERVER_ERROR \"Error\""));
}

Expand All @@ -180,7 +188,7 @@ void handleViolationException() throws Exception {

performRequest(DATA, MediaType.APPLICATION_JSON)
.andExpect(MockMvcResultMatchers.status().isBadRequest())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("PU_SIL_BAD_REQUEST"))
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("BAD_REQUEST"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Error"));
}
}

0 comments on commit adac2dc

Please sign in to comment.