Skip to content

Instantly share code, notes, and snippets.

@CarlLee
Created September 3, 2015 06:05
Show Gist options
  • Save CarlLee/145c54d4f5a6df123c03 to your computer and use it in GitHub Desktop.
Save CarlLee/145c54d4f5a6df123c03 to your computer and use it in GitHub Desktop.
package net.linni.webservice.controller.advice;
import net.linni.webservice.exception.AbstractRestAPIException;
import net.linni.webservice.exception.OperationFailureException;
import net.linni.webservice.exception.UnauthorizedOperationException;
import net.linni.webservice.model.Response;
import net.linni.webservice.util.ResponseCode;
import net.linni.webservice.util.ResponseFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MissingServletRequestParameterException;
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;
import org.testng.log4testng.Logger;
@ControllerAdvice
public class ExceptionHandlerControllerAdvice {
private static Logger sLogger = Logger
.getLogger(ExceptionHandlerControllerAdvice.class);
// @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
// @ExceptionHandler(Exception.class)
// public @ResponseBody
// Response<Void> handleException(Exception e) {
// sLogger.error("Exception: ", e.getCause());
// return ResponseFactory.makeResponse(ResponseFactory.OPERATION_FAILURE,
// null);
// }
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(AbstractRestAPIException.class)
public @ResponseBody
Response<Void> handlAbstractRestAPIException(AbstractRestAPIException e) {
sLogger.debug("AbstractRestAPIException: ", e);
return e.getRestResponse();
}
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(UnauthorizedOperationException.class)
public @ResponseBody
Response<Void> handlUnauthorizedOperationException(
UnauthorizedOperationException e) {
sLogger.debug("AbstractRestAPIException: ", e);
return e.getRestResponse();
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(OperationFailureException.class)
public @ResponseBody
Response<Void> handlOperationFailureException(OperationFailureException e) {
sLogger.debug("AbstractRestAPIException: ", e);
return e.getRestResponse();
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingServletRequestParameterException.class)
public @ResponseBody
Response<Void> handlMissingParamsException(
MissingServletRequestParameterException e) {
sLogger.debug("MissingParams: ", e);
return ResponseFactory.makeResponse(
ResponseCode.MISSING_REQUIRED_PARAMETER, null);
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(TypeMismatchException.class)
public @ResponseBody
Response<Void> handlTypeMismatchException(TypeMismatchException e) {
if (e.getCause() instanceof NumberFormatException) {
sLogger.debug("Wrong number format: ", e.getCause());
return ResponseFactory.makeResponse(
ResponseCode.INVALID_NUMBER_FORMAT, null);
} else {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment