Skip to content

Instantly share code, notes, and snippets.

@jittagornp
Created November 13, 2015 12:38
Show Gist options
  • Save jittagornp/ad46f1764b46de4db00f to your computer and use it in GitHub Desktop.
Save jittagornp/ad46f1764b46de4db00f to your computer and use it in GitHub Desktop.
package com.pamarin.rest.api.exception;
import com.pamarin.api.exception.ApiAuthorizationException;
import com.pamarin.api.exception.AuthorizationException;
import com.pamarin.api.exception.AuthenticationException;
import com.pamarin.api.exception.entity.AlreadyExistTripException;
import com.pamarin.api.exception.entity.InvalidFieldException;
import com.pamarin.api.exception.entity.CommentNotFoundException;
import com.pamarin.api.exception.entity.TripNotFoundException;
import com.pamarin.api.exception.entity.TripPostNotFoundException;
import com.pamarin.api.exception.entity.UserNotFoundException;
import com.pamarin.api.model.Error;
import java.io.FileNotFoundException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
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.servlet.NoHandlerFoundException;
/**
* @author jittagornp
*/
@ControllerAdvice
public class DefaultExceptionHandler {
private static final Logger LOG = LoggerFactory.getLogger(DefaultExceptionHandler.class);
@ResponseBody
@ExceptionHandler(BindException.class)
public Error http400(BindException ex) {
List<org.springframework.validation.FieldError> errors = ex.getFieldErrors();
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.BAD_REQUEST)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage("Field Validation fail.")
.setUserMessage("คำร้องขอผิดพลาด ลองตรวจสอบฟิลด์ข้อมูลที่ส่งมาดูอีกครั้ง")
.setFields(com.pamarin.api.model.FieldError.translates(errors))
.build();
}
@ResponseBody
@ExceptionHandler(MethodArgumentNotValidException.class)
public Error http400(MethodArgumentNotValidException ex) {
List<org.springframework.validation.FieldError> errors = ex.getBindingResult().getFieldErrors();
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.BAD_REQUEST)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage("Field Validation fail.")
.setUserMessage("คำร้องขอผิดพลาด ลองตรวจสอบฟิลด์ข้อมูลที่ส่งมาดูอีกครั้ง")
.setFields(com.pamarin.api.model.FieldError.translates(errors))
.build();
}
@ResponseBody
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public Error http400(HttpRequestMethodNotSupportedException ex) {
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.BAD_REQUEST)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage(ex.getMessage())
.setUserMessage("คำร้องขอผิดพลาด ลองตรวจสอบ url หรือข้อมูลที่แนบมาดูอีกครั้ง")
.build();
}
@ResponseBody
@ExceptionHandler(HttpMessageNotReadableException.class)
public Error http400(HttpMessageNotReadableException ex) {
return Error.builder()
//.setCode(ex.getCode())
.setStatus(HttpStatus.BAD_REQUEST)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage(ex.getMessage())
.setUserMessage("คำร้องขอผิดพลาด ลองตรวจสอบ url หรือข้อมูลที่แนบมาดูอีกครั้ง")
.build();
}
@ResponseBody
@ExceptionHandler(MissingServletRequestParameterException.class)
public Error http400(MissingServletRequestParameterException ex) {
return Error.builder()
//.setCode(ex.getCode())
.setStatus(HttpStatus.BAD_REQUEST)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage(ex.getMessage())
.setUserMessage("ลืมแนบ query string '" + ex.getParameterName() + "'")
.build();
}
@ResponseBody
@ExceptionHandler(AuthenticationException.class)
public Error http401(AuthenticationException ex) {
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.UNAUTHORIZED)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage(ex.getMessage())
.setUserMessage("ยังไม่ได้ทำการล็อกอิน")
.build();
}
@ResponseBody
@ExceptionHandler(AuthorizationException.class)
public Error http403(AuthorizationException ex) {
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.FORBIDDEN)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage(ex.getMessage())
.setUserMessage("ไม่มีสิทธิ์เข้าถึง")
.build();
}
@ResponseBody
@ExceptionHandler(ApiAuthorizationException.class)
public Error http403(ApiAuthorizationException ex) {
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.FORBIDDEN)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage(ex.getMessage())
.setUserMessage("ไม่มีสิทธิ์เข้าถึง api จาก " + ex.getReferer())
.build();
}
@ResponseBody
@ExceptionHandler(NoHandlerFoundException.class)
public Error http404(NoHandlerFoundException ex, HttpServletRequest request) {
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.NOT_FOUND)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage("Not found resource for request url " + request.getRequestURL().toString())
.setUserMessage("ไม่พบข้อมูล กรุณาตรวจสอบ url ใหม่อีกครั้ง")
.build();
}
@ResponseBody
@ExceptionHandler(FileNotFoundException.class)
public Error http404(FileNotFoundException ex) {
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.NOT_FOUND)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage(ex.getMessage())
.setUserMessage("ไม่พบไฟล์ข้อมูล กรุณาตรวจสอบ url ใหม่อีกครั้ง")
.build();
}
@ResponseBody
@ExceptionHandler(UserNotFoundException.class)
public Error http404(UserNotFoundException ex) {
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.NOT_FOUND)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage(ex.getMessage())
.setUserMessage("ไม่พบข้อมูลผู้ใช้ กรุณาตรวจสอบ url ใหม่อีกครั้ง")
.build();
}
@ResponseBody
@ExceptionHandler(TripNotFoundException.class)
public Error http404(TripNotFoundException ex) {
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.NOT_FOUND)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage(ex.getMessage())
.setUserMessage("ไม่พบข้อมูลทริป กรุณาตรวจสอบ url ใหม่อีกครั้ง")
.build();
}
@ResponseBody
@ExceptionHandler(TripPostNotFoundException.class)
public Error http404(TripPostNotFoundException ex) {
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.NOT_FOUND)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage(ex.getMessage())
.setUserMessage("ไม่พบข้อมูลโพสต์ กรุณาตรวจสอบ url ใหม่อีกครั้ง")
.build();
}
@ResponseBody
@ExceptionHandler(CommentNotFoundException.class)
public Error http404(CommentNotFoundException ex) {
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.NOT_FOUND)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage(ex.getMessage())
.setUserMessage("ไม่พบข้อมูลคอมเมนต์ กรุณาตรวจสอบ url ใหม่อีกครั้ง")
.build();
}
@ResponseBody
@ExceptionHandler(EmptyResultDataAccessException.class)
public Error http404(EmptyResultDataAccessException ex) {
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.NOT_FOUND)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage(ex.getMessage())
.setUserMessage("ไม่พบข้อมูล กรุณาตรวจสอบ url ใหม่อีกครั้ง")
.build();
}
@ResponseBody
@ExceptionHandler(InvalidFieldException.class)
public Error http500(InvalidFieldException ex) {
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.NOT_FOUND)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage(ex.getMessage())
.setUserMessage("ฟิลด์ข้อมูลไม่ถูกต้อง " + ex.getFieldName() + "(" + ex.getFieldValue() + ")")
.build();
}
@ResponseBody
@ExceptionHandler(AlreadyExistTripException.class)
public Error http500(AlreadyExistTripException ex) {
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.INTERNAL_SERVER_ERROR)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage(ex.getMessage())
.setUserMessage("มีข้อมูลทริป '" + ex.getTripname() + "' อยู่แล้ว ไม่สามารถบันทึกข้อมูลได้")
.build();
}
@ResponseBody
@ExceptionHandler(Exception.class)
public Error http500(Exception ex) {
LOG.warn(null, ex);
return Error.builder()
//.setCode(Integer.SIZE)
.setStatus(HttpStatus.INTERNAL_SERVER_ERROR)
.setType(ex.getClass().getSimpleName())
.setDeveloperMessage(ex.getMessage())
.setUserMessage("มีข้อผิดพลาดเกิดขึ้นในระหว่างการประมวลผล")
.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment