chore: add global REST error handling

This commit is contained in:
Pablo de la Torre Jamardo 2025-08-25 21:25:08 +02:00
parent 5215bf8779
commit 1033c96d65

View File

@ -0,0 +1,30 @@
package com.pablotj.portfolio.infrastructure.rest.config;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.WebRequest;
import java.util.Map;
@Controller
public class ApiErrorController implements ErrorController {
private final ErrorAttributes errorAttributes;
public ApiErrorController(ErrorAttributes errorAttributes) {
this.errorAttributes = errorAttributes;
}
@RequestMapping("/error")
public ResponseEntity<Map<String, Object>> handleError(WebRequest webRequest) {
Map<String, Object> attributes = errorAttributes.getErrorAttributes(webRequest,
ErrorAttributeOptions.defaults());
HttpStatus status = HttpStatus.valueOf((int) attributes.getOrDefault("status", 500));
return new ResponseEntity<>(attributes, status);
}
}