[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
}
```