[TOC]
# **1. 启用注解**
`@GoEnableException`
# **2. 异常码**
| 类型 | 异常 | 异常码 |
| --- | --- | --- |
| RUNTIME | 运行 | 1000|
| REQUEST | 请求 | 2000 |
| VALI | 效验 | 3000 |
| DBASE | 数据库 | 4000 |
| TOKEN | 令牌 | 5000 |
| SIGN | 签名 | 6000 |
| RETRY | 重试 | 7000 |
| LIMITER | 限流 | 8000 |
| UNKNOWN | 未知 | 9999 |
# **3. 注意事项**
遇到某些异常会暴露出包名、类名、方法名,请根据实际场景单独处理
```
@RequestMapping(value = "save")
public Result save(BaseVo base, Member req, Model model) {
try {
// 处理业务逻辑
return R.succ();
} catch (Exception e) {
throw new RunException(RunExc.RUNTIME, "保存异常");
}
}
```
```
{"code":1000,"msg":"保存异常","success":false}
```
# **4. 示例说明**
## **4.1 运行时**
当出现NullPointerException时
```
@RequestMapping(value = "runtime")
public void runtime() {
log.debug("空指针,抛 {} 异常", NullPointerException.class);
throw new NullPointerException("手动抛空指针异常");
}
```
```
{
"code": 1000,
"msg": "手动抛空指针异常",
"status": false
}
```
## **4.2 请求时**
当以GET方式请求POST方法时
```
@RequestMapping(value = "request", method = RequestMethod.POST)
public void request() {
log.debug("非post请求,抛 {} 异常", HttpRequestMethodNotSupportedException.class);
}
```
```
{
"code": 2000,
"msg": "不支持当前请求方法",
"status": false
}
```
## **4.3 效验时**
当传递参数为空时,这里分hibernate、spring为2种效验方式
### **4.3.1 Hibernate Validator**
```
@RequestMapping(value = "hibernate/validator")
public void validator(@NotBlank String p) {
log.debug("参数空,抛 {} 异常", ConstraintViolationException.class);
}
```
```
{
"code": 3000,
"msg": "校验错误",
"data": [
"validator.p 不能为空"
],
"status": false
}
```
### **4.3.2 Spring Validator**
```
@RequestMapping(value = "spring/validator")
public void validator(@Validated BaseVo base) {
log.debug("参数空,抛 {} 异常", BindException.class);
}
```
```
{
"code": 3000,
"msg": "校验错误",
"data": [
"key 不能为null"
],
"status": false
}
```
## **4.3 数据库**
```
@RequestMapping(value = "database")
public void database() {
log.debug("无此表,抛 {} 异常", SQLSyntaxErrorException.class);
JdbcTemplatePlus.queryForMap("select * from xx_test");// 不捕获
JdbcTemplatePlus.get().queryForMap("select * from xx_test", Maps.newConcurrentMap());// 全局捕获
}
```
```
{
"code": 4000,
"msg": "PreparedStatementCallback; bad SQL grammar [select * from xx_test]; nested exception is java.sql.SQLSyntaxErrorException: Table 'test.xx_test' doesn't exist",
"status": false
}
```
## **4.4 自定义**
```
@RequestMapping(value = "custom")
public void custom() {
log.debug("自定义,抛 {} 异常", RunException.class);
throw new RunException(RunExc.SIGN);
}
```
```
{
"code": 6000,
"msg": "签名错误",
"status": false
}
```