-
Notifications
You must be signed in to change notification settings - Fork 19
/
ExceptionTranslator.java
50 lines (40 loc) · 1.51 KB
/
ExceptionTranslator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.lccf.exception;
import com.lccf.util.ResponseVo;
import com.lccf.util.ResponseVoUtil;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lccf.service.security.UserNotActivatedException;
@ControllerAdvice
public class ExceptionTranslator {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
@ExceptionHandler(value = Exception.class)
@ResponseBody
public ResponseVo loginException(Exception e, HttpServletRequest request) {
e.printStackTrace();
logger.error(e.getMessage());
String exceptionMsg = getException(e);
return ResponseVoUtil.failResult(exceptionMsg);
}
/**
* 获取错误信息
*
* @param exception
* @return
*/
private String getException(Exception exception) {
if (exception instanceof UserNotActivatedException) {
return ExceptionConst.USER_NOT_ACTIVATE;
} else if (exception instanceof UsernameNotFoundException) {
return ExceptionConst.USER_NOT_FOUND;
} else {
return ExceptionConst.SYS_EXCEPTION;
}
}
}