Commit 5aaa6cf7 authored by 马超's avatar 马超

新增业务异常使用方式

parent df16100a
...@@ -882,4 +882,51 @@ isOut字段标识接口类型,false:外部系统调用本系统,true:本 ...@@ -882,4 +882,51 @@ isOut字段标识接口类型,false:外部系统调用本系统,true:本
``` ```
## 七、业务异常处理
LDP业务异常类是`BizException`,最简单的使用方式就是,传入code和message,并抛出BizException
```java
try{
......
......
} catch{
throw new BizException(500,"业务异常")
}
//或者
if(condition) {
throw new BizException(500,"业务异常")
}
```
有时候我们需要考虑国际化,需要在startup模块下的resources目录中国际化文件中写上对应的code和message,当浏览器为英文时,使用en_US文件中的code和message,否则使用zh_CN文件,如果两个文件都没写,则默认使用**messages.properties**中的code以及message。
![bizException](../imgs/biz-exception.png)
写好code和message后,注入MessageSourceHandler,并调用newBizException方法,传入去掉code和message的错误编码。以下是样例,如果删除的id值为“error-1”则抛出业务异常(参考脚手架工程ExampleRest类)。
```java
@Autowired
MessageSourceHandler messageSourceHandler;
/**
* 删除用户
*
* @param id
* @return
*/
@PostMapping("/del/{id}")
public RestResult deleteUser(@PathVariable("id") String id) {
if ("error-1".equals(id)) {
throw messageSourceHandler.newBizException("example.biz.demo.del.error");
}
ExampleUserInfo userInfo = new ExampleUserInfo(id);
exampleService.delete(userInfo);
return new RestResult(ResultMsg.SUCCESS, "");
}
```
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment