Step-1
Define some custom exception :-
public class LanguageNotFoundException extends RuntimeException {
public LanguageNotFoundException(String message){
super(message);
}
public LanguageNotFoundException(String message,Exception exe){
super(message,exe);
}
}
public class UserTokenNotValidException extends RuntimeException {
private String userToken;
public String getUserToken() {
return userToken;
}
public void setUserToken(String userToken) {
this.userToken = userToken;
}
}
Step-2
@ControllerAdvice ->>>Spring 3.2
@ControllerAdvice
public class GPSGlobalExceptionHandler {
@Autowired
@Qualifier("UserService")
private UserService userService;
/**
* Initiate Logger for this class
*/
private static final Log logger = LogFactory.getLog(GPSGlobalExceptionHandler.class);
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(LanguageNotFoundException.class)
@ResponseBody QuizErrorInfo handleLanguageException(HttpServletRequest req, Exception ex) {
QuizErrorInfo errorInfo=new QuizErrorInfo(HttpStatus.BAD_REQUEST.toString(),"4000" , ex.getMessage(), ex.getClass().toString(), req.getRequestURL().toString());
errorDBLogger(ex, req.getRequestURL().toString());
ex.printStackTrace();
if(logger.isErrorEnabled()){
logger.error(errorInfo);
logger.error(ex.getStackTrace());
}
return errorInfo;
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public QuizErrorInfo processValidationError(HttpServletRequest req,MethodArgumentNotValidException ex) {
errorDBLogger(ex, req.getRequestURL().toString());
BindingResult result = ex.getBindingResult();
List<FieldError> fieldErrors = result.getFieldErrors();
String errors= processFieldErrors(fieldErrors);
QuizErrorInfo errorInfo=new QuizErrorInfo(HttpStatus.BAD_REQUEST.toString(),"3000" , errors, ex.getClass().toString(), req.getRequestURL().toString());
return errorInfo;
}
}
Step-3
public class QuizErrorInfo {
private String status;
private String code;
private String message;
private String ex;
private String moreInfo;
public QuizErrorInfo(String status, String code, String message, String ex,
String moreInfo) {
this.status = status;
this.code = code;
this.message = message;
this.ex = ex;
this.moreInfo = moreInfo;
}
}
Define some custom exception :-
public class LanguageNotFoundException extends RuntimeException {
public LanguageNotFoundException(String message){
super(message);
}
public LanguageNotFoundException(String message,Exception exe){
super(message,exe);
}
}
public class UserTokenNotValidException extends RuntimeException {
private String userToken;
public String getUserToken() {
return userToken;
}
public void setUserToken(String userToken) {
this.userToken = userToken;
}
}
Step-2
@ControllerAdvice ->>>Spring 3.2
@ControllerAdvice
public class GPSGlobalExceptionHandler {
@Autowired
@Qualifier("UserService")
private UserService userService;
/**
* Initiate Logger for this class
*/
private static final Log logger = LogFactory.getLog(GPSGlobalExceptionHandler.class);
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(LanguageNotFoundException.class)
@ResponseBody QuizErrorInfo handleLanguageException(HttpServletRequest req, Exception ex) {
QuizErrorInfo errorInfo=new QuizErrorInfo(HttpStatus.BAD_REQUEST.toString(),"4000" , ex.getMessage(), ex.getClass().toString(), req.getRequestURL().toString());
errorDBLogger(ex, req.getRequestURL().toString());
ex.printStackTrace();
if(logger.isErrorEnabled()){
logger.error(errorInfo);
logger.error(ex.getStackTrace());
}
return errorInfo;
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public QuizErrorInfo processValidationError(HttpServletRequest req,MethodArgumentNotValidException ex) {
errorDBLogger(ex, req.getRequestURL().toString());
BindingResult result = ex.getBindingResult();
List<FieldError> fieldErrors = result.getFieldErrors();
String errors= processFieldErrors(fieldErrors);
QuizErrorInfo errorInfo=new QuizErrorInfo(HttpStatus.BAD_REQUEST.toString(),"3000" , errors, ex.getClass().toString(), req.getRequestURL().toString());
return errorInfo;
}
}
Step-3
public class QuizErrorInfo {
private String status;
private String code;
private String message;
private String ex;
private String moreInfo;
public QuizErrorInfo(String status, String code, String message, String ex,
String moreInfo) {
this.status = status;
this.code = code;
this.message = message;
this.ex = ex;
this.moreInfo = moreInfo;
}
}
No comments:
Post a Comment