Please help.
How to throw exception from service in middleware with 429 error?
When I make in service method throw new HttpClientErrorException(HttpStatus.TOO_MANY_REQUESTS);
I receive HTTP 500 error from tomcat.
Hi,
you may try to create your own exception handler that will process your exception and return a ResponseEntity with a proper http status. See this topic: REST API custom exception handling - CUBA.Platform
public class CustomRestControllerExceptionHandler {
private Logger log = LoggerFactory.getLogger(CustomRestControllerExceptionHandler.class);
@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseEntity<ErrorInfo> handleException(Exception e) {
log.error("Exception in REST controller", e);
ErrorInfo errorInfo = new ErrorInfo("Server error", e.getMessage());
if (e instanceof RestAPIException) {
String details = ((RestAPIException) e).getDetails();
HttpStatus status = ((RestAPIException) e).getHttpStatus();
ErrorInfo server_error = new ErrorInfo( e.getMessage(), details);
if (details.contains("403")) {
status = HttpStatus.FORBIDDEN;
}
if (details.contains("429")) {
status = HttpStatus.TOO_MANY_REQUESTS;
}
return new ResponseEntity<>(server_error, status);
}
return new ResponseEntity<>(errorInfo, HttpStatus.INTERNAL_SERVER_ERROR);
}
May be exist more conventient way to get HttpsStatus from fired exception?