|
@@ -1,35 +1,44 @@
|
|
|
package com.pharmacopoeia.config;
|
|
package com.pharmacopoeia.config;
|
|
|
|
|
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.pharmacopoeia.dto.ApiResponse;
|
|
import com.pharmacopoeia.dto.ApiResponse;
|
|
|
|
|
+import jakarta.servlet.http.HttpServletResponse;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
+import org.springframework.http.MediaType;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
-import org.springframework.web.bind.annotation.ResponseStatus;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+
|
|
|
@RestControllerAdvice
|
|
@RestControllerAdvice
|
|
|
public class GlobalExceptionHandler {
|
|
public class GlobalExceptionHandler {
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
|
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
|
|
|
|
+ private static final ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
|
|
|
@ExceptionHandler(IllegalArgumentException.class)
|
|
@ExceptionHandler(IllegalArgumentException.class)
|
|
|
- @ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
|
|
- public ApiResponse<Void> handleBadRequest(IllegalArgumentException e) {
|
|
|
|
|
- return ApiResponse.error(400, e.getMessage());
|
|
|
|
|
|
|
+ public void handleBadRequest(IllegalArgumentException e, HttpServletResponse response) throws IOException {
|
|
|
|
|
+ writeJson(response, HttpStatus.BAD_REQUEST, ApiResponse.error(400, e.getMessage()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@ExceptionHandler(RuntimeException.class)
|
|
@ExceptionHandler(RuntimeException.class)
|
|
|
- @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
|
- public ApiResponse<Void> handleRuntime(RuntimeException e) {
|
|
|
|
|
|
|
+ public void handleRuntime(RuntimeException e, HttpServletResponse response) throws IOException {
|
|
|
log.error("Runtime error: {}", e.getMessage(), e);
|
|
log.error("Runtime error: {}", e.getMessage(), e);
|
|
|
- return ApiResponse.error(500, e.getMessage());
|
|
|
|
|
|
|
+ writeJson(response, HttpStatus.INTERNAL_SERVER_ERROR, ApiResponse.error(500, e.getMessage()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@ExceptionHandler(Exception.class)
|
|
@ExceptionHandler(Exception.class)
|
|
|
- @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
|
- public ApiResponse<Void> handleGeneral(Exception e) {
|
|
|
|
|
|
|
+ public void handleGeneral(Exception e, HttpServletResponse response) throws IOException {
|
|
|
log.error("Unexpected error: {}", e.getMessage(), e);
|
|
log.error("Unexpected error: {}", e.getMessage(), e);
|
|
|
- return ApiResponse.error(500, "Internal server error");
|
|
|
|
|
|
|
+ writeJson(response, HttpStatus.INTERNAL_SERVER_ERROR, ApiResponse.error(500, "Internal server error"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void writeJson(HttpServletResponse response, HttpStatus status, ApiResponse<?> body) throws IOException {
|
|
|
|
|
+ response.setStatus(status.value());
|
|
|
|
|
+ response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
|
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
|
|
+ mapper.writeValue(response.getWriter(), body);
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|