org.sklsft.commons.rest.exception.RestExceptionHandler Maven / Gradle / Ivy
package org.sklsft.commons.rest.exception;
import org.sklsft.commons.api.exception.ApplicationException;
import org.sklsft.commons.api.exception.ErrorReport;
import org.sklsft.commons.api.exception.TechnicalError;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* How to handle exceptions :
* If it is an {@link ApplicationException}, it will be serialized to an {@link ErrorReport}
* Else, an unknown {@link TechnicalError} will replace it for serialization.
*
* @author Nicolas Thibault
*
*/
@ControllerAdvice
public class RestExceptionHandler {
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(ApplicationException.class)
public @ResponseBody ErrorReport handleApplicationException(ApplicationException e) {
ErrorReport errorReport = new ErrorReport();
errorReport.setExceptionClassName(e.getClass().getName());
errorReport.setMessage(e.getMessage());
return errorReport;
}
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public @ResponseBody ErrorReport handleException(Exception e) {
ErrorReport errorReport = new ErrorReport();
errorReport.setExceptionClassName(TechnicalError.class.getName());
errorReport.setMessage(ApplicationException.ERROR_UNKNOWN);
return errorReport;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy