Skip to content
Snippets Groups Projects
Commit 92c3e471 authored by Lukas Karsch's avatar Lukas Karsch
Browse files

#32 handling growbros exception and internal server error

parent b9e4d485
No related branches found
No related tags found
1 merge request!20Resolve "Error handling für controller"
package hdm.mi.growbros.controllers;
import hdm.mi.growbros.exceptions.GrowBrosException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ControllerAdvice
public class RestErrorHandler extends ResponseEntityExceptionHandler {
private final static Logger log = LoggerFactory.getLogger(RestErrorHandler.class);
@ExceptionHandler(GrowBrosException.class)
protected ResponseEntity<String> handleGrowBrosException(
GrowBrosException e
) {
log.error("Handling exception '{}' : {} (Status code {})", e.getClass(), e.getMessage(), e.getStatusCode());
return ResponseEntity.status(e.getStatusCode()).body(e.getMessage());
}
@ExceptionHandler
protected ResponseEntity<Object> handleInternalServerError(
RuntimeException e,
WebRequest req
) {
log.error("Handling internal server error: {}", e.getClass());
log.error("Stacktrace:", e);
return handleExceptionInternal(e, "Internal server error", new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, req);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment