fix(api): handle exceptions with status codes (#2160)
This commit is contained in:
@@ -4,6 +4,7 @@ import jakarta.validation.ConstraintViolationException;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.catalina.connector.ClientAbortException;
|
import org.apache.catalina.connector.ClientAbortException;
|
||||||
import org.springframework.dao.DataIntegrityViolationException;
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
@@ -115,8 +116,28 @@ public class GlobalExceptionHandler {
|
|||||||
|
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
public ResponseEntity<ErrorResponse> handleGenericException(Exception ex) {
|
public ResponseEntity<ErrorResponse> handleGenericException(Exception ex) {
|
||||||
|
int statusCode = HttpStatus.INTERNAL_SERVER_ERROR.value();
|
||||||
|
HttpHeaders headers = HttpHeaders.EMPTY;
|
||||||
|
ErrorResponse errorResponse = new ErrorResponse(
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
||||||
|
"An unexpected error occurred."
|
||||||
|
);
|
||||||
|
|
||||||
|
if (ex instanceof org.springframework.web.ErrorResponse er) {
|
||||||
|
statusCode = er.getStatusCode().value();
|
||||||
|
headers = er.getHeaders();
|
||||||
|
errorResponse = new ErrorResponse(
|
||||||
|
er.getBody().getStatus(),
|
||||||
|
er.getBody().getDetail()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
log.error("Unhandled exception: {}", ex.getMessage(), ex);
|
log.error("Unhandled exception: {}", ex.getMessage(), ex);
|
||||||
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "An unexpected error occurred.");
|
|
||||||
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(
|
||||||
|
errorResponse,
|
||||||
|
headers,
|
||||||
|
statusCode
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package org.booklore.exception;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.web.HttpMediaTypeNotAcceptableException;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||||
|
|
||||||
|
public class GlobalExceptionHandlerTest {
|
||||||
|
@Test
|
||||||
|
void handleGenericException_shouldUseSpringErrorResponse() {
|
||||||
|
var ex = new HttpMediaTypeNotAcceptableException(List.of(MediaType.APPLICATION_OCTET_STREAM));
|
||||||
|
var handler = new GlobalExceptionHandler();
|
||||||
|
var response = handler.handleGenericException(ex);
|
||||||
|
|
||||||
|
assertThat(response.getStatusCode().value()).isEqualTo(406);
|
||||||
|
assertThat(response.getBody()).isNotNull();
|
||||||
|
assertThat(response.getBody().getStatus()).isEqualTo(406);
|
||||||
|
assertThat(response.getBody().getMessage()).isEqualTo("Acceptable representations: [application/octet-stream].");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user