Skip to content

Commit

Permalink
# add Exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
顾鲍尔 authored and 顾鲍尔 committed Jul 2, 2017
1 parent 8ffd2d3 commit 0227297
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/main/java/com/boylegu/springboot_vue/dao/ErrorInfo.java
@@ -0,0 +1,57 @@
package com.boylegu.springboot_vue.dao;

/**
* Created by gubaoer on 17/7/2.
*/

public class ErrorInfo<T> {

public static final Integer OK = 0;
public static final Integer ERROR = 100;

private Integer code;
private String message;
private String url;
private T data;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public static Integer getOK() {
return OK;
}

public static Integer getERROR() {
return ERROR;
}

public Integer getCode() {
return code;
}

public void setCode(Integer code) {
this.code = code;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}

}
@@ -0,0 +1,12 @@
package com.boylegu.springboot_vue.exception;

/**
* Created by gubaoer on 17/7/2.
*/


public class ProjException extends Exception {
public ProjException(String message) {
super(message);
}
}
@@ -0,0 +1,38 @@
package com.boylegu.springboot_vue.exception;

/**
* Created by gubaoer on 17/7/2.
*/

import com.boylegu.springboot_vue.dao.ErrorInfo;
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.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class ProjExceptionHandler {

@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
ModelAndView mav = new ModelAndView();
mav.addObject("exception", e);
mav.addObject("url", req.getRequestURL());
mav.setViewName("error");
return mav;
}

@ExceptionHandler(value = ProjException.class)
@ResponseBody
public ErrorInfo<String> jsonErrorHandler(HttpServletRequest req, ProjException e) throws Exception {
ErrorInfo<String> r = new ErrorInfo<>();
r.setMessage(e.getMessage());
r.setCode(ErrorInfo.ERROR);
r.setData("Some Data");
r.setUrl(req.getRequestURL().toString());
return r;
}

}

0 comments on commit 0227297

Please sign in to comment.