Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yanxuan-qc-change-system
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
胡园园
yanxuan-qc-change-system
Commits
4bcc2eca
Commit
4bcc2eca
authored
Nov 11, 2022
by
“zcwang”
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init: 异常处理
parent
741b883d
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
506 additions
and
0 deletions
+506
-0
AjaxResult.java
.../com/netease/mail/yanxuan/change/biz/meta/AjaxResult.java
+73
-0
ResponseCode.java
...om/netease/mail/yanxuan/change/biz/meta/ResponseCode.java
+187
-0
ExceptionFactory.java
...l/yanxuan/change/biz/meta/exception/ExceptionFactory.java
+28
-0
ServiceException.java
...l/yanxuan/change/biz/meta/exception/ServiceException.java
+49
-0
RequestUtil.java
.../netease/mail/yanxuan/change/common/util/RequestUtil.java
+51
-0
ResponseUtil.java
...netease/mail/yanxuan/change/common/util/ResponseUtil.java
+30
-0
GlobalExceptionHandler.java
...e/mail/yanxuan/change/web/aop/GlobalExceptionHandler.java
+88
-0
No files found.
yanxuan-qc-change-system-biz/src/main/java/com/netease/mail/yanxuan/change/biz/meta/AjaxResult.java
0 → 100644
View file @
4bcc2eca
package
com
.
netease
.
mail
.
yanxuan
.
change
.
biz
.
meta
;
import
java.util.LinkedHashMap
;
/**
* @author lwtang
*/
public
class
AjaxResult
extends
LinkedHashMap
<
String
,
Object
>
{
private
static
final
long
serialVersionUID
=
5451316194035153776L
;
public
AjaxResult
()
{
setCode
(
ResponseCode
.
SUCCESS
);
}
public
AjaxResult
(
int
code
,
String
msg
)
{
setCodeInt
(
code
);
put
(
MSG
,
msg
);
}
public
AjaxResult
(
ResponseCode
responseCode
)
{
setCode
(
responseCode
);
}
public
AjaxResult
(
ResponseCode
responseCode
,
Object
result
)
{
setCode
(
responseCode
);
put
(
RESULT
,
result
);
}
/**
* 响应code,默认<code>ResponseCode.SUCCESS</code>
*/
public
static
final
String
CODE
=
"code"
;
/**
* 消息
*/
public
static
final
String
MSG
=
"msg"
;
/**
* 结果
*/
public
static
final
String
RESULT
=
"data"
;
public
void
setCode
(
ResponseCode
code
)
{
put
(
CODE
,
code
.
getCode
());
}
public
void
setMsg
(
String
msg
)
{
put
(
MSG
,
msg
);
}
public
void
setCodeInt
(
int
code
)
{
put
(
CODE
,
code
);
}
public
ResponseCode
getCode
()
{
return
(
ResponseCode
)
get
(
CODE
);
}
public
Object
getResult
()
{
return
(
String
)
get
(
RESULT
);
}
public
void
setResult
(
Object
result
)
{
put
(
RESULT
,
result
);
}
public
static
AjaxResult
with
(
Object
result
)
{
return
new
AjaxResult
(
ResponseCode
.
SUCCESS
,
result
);
}
}
yanxuan-qc-change-system-biz/src/main/java/com/netease/mail/yanxuan/change/biz/meta/ResponseCode.java
0 → 100644
View file @
4bcc2eca
package
com
.
netease
.
mail
.
yanxuan
.
change
.
biz
.
meta
;
/**
* 返回的code
*
* @author lwtang
*/
public
enum
ResponseCode
{
//成功
SUCCESS
(
200
,
"SUCCESS"
),
// 结果、数据为空
EMPTY
(
201
,
"EMPTY"
),
// 处理中
PROCESSING
(
202
,
"PROCESSING"
),
//已存在
HAVE_EXIST
(
203
,
"用户{0}存在未完成任务"
),
//商品已售罄
SELL_OUT
(
210
,
"SELL_OUT"
),
//已加购过该福袋
HAVE_ADD
(
211
,
"HAVE_ADD"
),
//购物车已满
IS_FULL
(
212
,
"IS_FULL"
),
//拥有补签卡达上限
CARD_FULL
(
213
,
"CARD_FULL"
),
NO_TITLE_INFO
(
214
,
"标题信息缺失"
),
QUERY_FILL_CHECK_CARDS_EXCEPTION
(
215
,
"获取补签卡异常"
),
SKU_NO_EXIST
(
216
,
"skuId不存在"
),
HAVE_SIGN
(
217
,
"签到失败"
),
NO_IN_HAND_TASK
(
218
,
"没有进行中的任务"
),
NO_TASK_ID
(
219
,
"该任务不存在"
),
TASK_AWARD_HAVE_CASH
(
220
,
"任务奖励已兑换"
),
ACTIVITY_CLOSE
(
221
,
"活动关闭"
),
N0_LOTTERY
(
222
,
"无抽奖资格"
),
NO_LOTTERY_COUNT
(
223
,
"无抽奖次数"
),
LOTTERY_ERROR
(
224
,
"抽奖失败"
),
SEND_RED_PACKED_FAIL
(
225
,
"发红包失败"
),
NO_SELECT_GOOD_POWER
(
226
,
"没有选商品的资格"
),
SEND_GIFT_CARD_FAIL
(
227
,
"发福利卡失败"
),
SEND_COUPON_FAIL
(
228
,
"发优惠券失败"
),
SEND_POINT_FAIL
(
229
,
"发积分失败"
),
SEND_BACK_GOLD_FAIL
(
230
,
"发回馈金失败"
),
NO_WAIT_RECEIVE
(
231
,
"无待领取"
),
PARAM_MISS
(
232
,
"参数缺失"
),
//----------3xx-----------
//----------4xx-----------
//非法参数.
BAD_REQUEST
(
400
,
"BAD REQUEST"
),
//未登录
NOT_LOGIN
(
401
,
"NO UID"
),
//重复提交
APPLYED
(
402
,
"APPLYED"
),
// 禁用,token验证失败等; 内部接口认证:非法访问
FORBIDDEN
(
403
,
"FORBIDDEN"
),
// 未找到资源,具体跟业务场景有关,比如红包不存在
NOT_FOUND
(
404
,
"NOT FOUND"
),
// 没有资格,如等级不够
UNQULIFIED
(
405
,
"UNQULIFIED"
),
// 资源不够
RUNOUT
(
406
,
"RUNOUT"
),
// 请求受限、余额不足
LIMIT
(
407
,
"LIMIT"
),
//该crmId在数据库已存在
SAME_CRM_ID
(
408
,
"该crmId在数据库已存在"
),
// 风控异常, UGC检查失败等
RISK
(
411
,
"RISK"
),
UNIQUE_KEY
(
412
,
"唯一索引异常"
),
//----------5xx-----------
// 服务器错误: db异常等
SERVER_ERROR
(
500
,
"SERVER ERROR"
),
//----------others-----------
// 限流
RATE_LIMIT
(
700
,
"RATE_LIMIT"
),
THIRD_ERR
(
800
,
"rpc error"
),
CANT_USE
(
801
,
"激活码id 不可用"
),
NO_GOOD_TASK
(
802
,
"不是商品任务"
),
CONFIG_ERROR
(
1001
,
"配置错误"
),
//----------10xx-----------
NO_DATA
(
1007
,
"无数据"
),
STEP_NOT_ENOUGH
(
1008
,
"步数不足"
),
ALREADY_DO
(
1009
,
"已完成"
),
NOT_FINISH
(
1011
,
"未完成"
),
FINISH_GUIDE
(
1012
,
"完成新手奖励"
),
RECEIVE_GUIDE
(
1013
,
"完成新手奖励"
),
NOT_IN_DMP
(
1014
,
"未命中DMP分组"
),
NOT_BIND_PHONE
(
1015
,
"未绑定手机号"
),
/**
* 名字已经被使用
*/
NAME_EXIST
(
2000
,
"NAME_IS_USED"
),
/**
* 无权限
*/
NO_PERMISSION
(
2001
,
"无权限"
),
CAN_NOT_DEL
(
2002
,
"不可删除"
),
UPDATE_FAIL
(
2003
,
"更改失败"
),
CANT_FOUND
(
2004
,
"查询不到"
),
SEND_FAIL
(
2005
,
"发送失败"
),
ACT_END
(
2006
,
"活动结束"
),
/**
* 操作记录不存在
*/
OP_RECORD_NOT_EXIST
(
3000
,
"opRecord id not existed"
),
/**
* 只有操作成功才允许进行验证
*/
ONLY_OP_SUCCESS_ALLOWED
(
3001
,
"verify only allowed when op success"
);
private
int
code
;
private
String
msg
;
ResponseCode
(
int
code
,
String
msg
)
{
this
.
code
=
code
;
this
.
msg
=
msg
;
}
public
int
getCode
()
{
return
code
;
}
public
String
getMsg
()
{
return
msg
;
}
}
yanxuan-qc-change-system-biz/src/main/java/com/netease/mail/yanxuan/change/biz/meta/exception/ExceptionFactory.java
0 → 100644
View file @
4bcc2eca
package
com
.
netease
.
mail
.
yanxuan
.
change
.
biz
.
meta
.
exception
;
import
com.netease.mail.yanxuan.change.biz.meta.ResponseCode
;
/**
* @author: bndong
* @date: 2021/5/25
*/
public
class
ExceptionFactory
{
public
static
ServiceException
createBiz
(
ResponseCode
resCode
)
{
return
new
ServiceException
(
resCode
);
}
public
static
ServiceException
createBiz
(
ResponseCode
resCode
,
String
message
)
{
return
new
ServiceException
(
resCode
,
message
);
}
public
static
ServiceException
createNotLogin
()
{
return
createBiz
(
ResponseCode
.
NOT_LOGIN
);
}
public
static
ServiceException
createParamFail
()
{
return
createBiz
(
ResponseCode
.
BAD_REQUEST
);
}
}
yanxuan-qc-change-system-biz/src/main/java/com/netease/mail/yanxuan/change/biz/meta/exception/ServiceException.java
0 → 100644
View file @
4bcc2eca
package
com
.
netease
.
mail
.
yanxuan
.
change
.
biz
.
meta
.
exception
;
import
com.netease.mail.yanxuan.change.biz.meta.ResponseCode
;
/**
* @author: bndong
* @date: 2021/5/25
*/
public
class
ServiceException
extends
RuntimeException
{
private
static
final
long
serialVersionUID
=
2531549421457088715L
;
private
ResponseCode
code
=
ResponseCode
.
SUCCESS
;
private
String
msg
;
public
ServiceException
(
ResponseCode
code
,
String
msg
)
{
super
(
"code:"
+
code
+
",msg:"
+
msg
);
this
.
msg
=
msg
;
this
.
code
=
code
;
}
public
ServiceException
(
ResponseCode
code
)
{
super
(
"code:"
+
code
.
getCode
()
+
",msg:"
+
code
.
getMsg
());
this
.
code
=
code
;
this
.
msg
=
code
.
getMsg
();
}
public
ResponseCode
getCode
()
{
return
code
;
}
public
int
getIntCode
()
{
return
code
.
getCode
();
}
public
void
setCode
(
ResponseCode
code
)
{
this
.
code
=
code
;
}
public
String
getMsg
()
{
return
msg
;
}
public
void
setMsg
(
String
msg
)
{
this
.
msg
=
msg
;
}
}
yanxuan-qc-change-system-common/src/main/java/com/netease/mail/yanxuan/change/common/util/RequestUtil.java
0 → 100644
View file @
4bcc2eca
package
com
.
netease
.
mail
.
yanxuan
.
change
.
common
.
util
;
import
java.io.BufferedReader
;
import
javax.servlet.http.HttpServletRequest
;
import
org.springframework.util.StringUtils
;
/**
* @author cyZhao
* @date 2019/1/28
*/
public
class
RequestUtil
{
public
static
final
String
CHAR_SET
=
"UTF-8"
;
private
static
final
String
POST_METHOD
=
"POST"
;
public
static
String
getRequestPath
(
HttpServletRequest
request
)
{
if
(
request
==
null
)
{
return
null
;
}
String
path
=
request
.
getRequestURI
();
String
queryStr
=
request
.
getQueryString
();
if
(!
StringUtils
.
isEmpty
(
queryStr
))
{
path
=
path
+
"?"
+
queryStr
;
}
if
(
POST_METHOD
.
equals
(
request
.
getMethod
().
toUpperCase
()))
{
String
body
=
getRequestBody
(
request
);
if
(!
StringUtils
.
isEmpty
(
body
))
{
path
+=
"\nbody: "
+
body
;
}
}
return
path
;
}
private
static
String
getRequestBody
(
HttpServletRequest
request
)
{
StringBuffer
json
=
new
StringBuffer
();
String
line
;
try
{
BufferedReader
reader
=
request
.
getReader
();
while
((
line
=
reader
.
readLine
())
!=
null
)
{
json
.
append
(
line
);
}
}
catch
(
Exception
e
)
{
}
return
json
.
toString
();
}
}
yanxuan-qc-change-system-common/src/main/java/com/netease/mail/yanxuan/change/common/util/ResponseUtil.java
0 → 100644
View file @
4bcc2eca
package
com
.
netease
.
mail
.
yanxuan
.
change
.
common
.
util
;
import
java.io.IOException
;
import
javax.servlet.http.HttpServletResponse
;
import
com.alibaba.fastjson.JSONObject
;
/**
* @author cyZhao
* @date 2019/1/28
*/
public
class
ResponseUtil
{
/**
* 输出json
*/
public
static
void
writeJson
(
HttpServletResponse
response
,
String
json
)
throws
IOException
{
response
.
setContentType
(
"application/json;charset=UTF-8"
);
response
.
setHeader
(
"CACHE-CONTROL"
,
"no-aspect"
);
response
.
getWriter
().
write
(
json
);
response
.
flushBuffer
();
}
/**
* 输出json
*/
public
static
void
writeJson
(
HttpServletResponse
response
,
Object
json
)
throws
IOException
{
writeJson
(
response
,
JSONObject
.
toJSONString
(
json
));
}
}
yanxuan-qc-change-system-web/src/main/java/com/netease/mail/yanxuan/change/web/aop/GlobalExceptionHandler.java
0 → 100644
View file @
4bcc2eca
package
com
.
netease
.
mail
.
yanxuan
.
change
.
web
.
aop
;
import
java.io.IOException
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
org.apache.tomcat.util.http.ResponseUtil
;
import
org.springframework.beans.TypeMismatchException
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.util.StringUtils
;
import
org.springframework.validation.FieldError
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.MissingServletRequestParameterException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
com.alibaba.fastjson.JSON
;
import
com.netease.mail.yanxuan.change.biz.meta.AjaxResult
;
import
com.netease.mail.yanxuan.change.biz.meta.exception.ServiceException
;
import
com.netease.mail.yanxuan.change.common.util.RequestUtil
;
import
lombok.extern.slf4j.Slf4j
;
@ControllerAdvice
@Slf4j
public
class
GlobalExceptionHandler
{
@ExceptionHandler
(
value
=
Exception
.
class
)
public
void
handle
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Exception
ex
)
throws
IOException
{
String
pathInfo
=
RequestUtil
.
getRequestPath
(
request
);
if
(
ex
instanceof
ServiceException
)
{
log
.
error
(
"biz error on handle request: {},{}"
,
pathInfo
,
((
ServiceException
)
ex
).
getMsg
());
}
else
if
(
ex
instanceof
TypeMismatchException
)
{
ex
=
new
IllegalArgumentException
(
"参数"
+
((
TypeMismatchException
)
ex
).
getRequiredType
()
+
"类型不匹配"
);
log
.
error
(
"handleException pathInfo:{},param:{}"
,
pathInfo
,
JSON
.
toJSONString
(
request
.
getParameterMap
()),
ex
);
}
else
if
(
ex
instanceof
MissingServletRequestParameterException
)
{
ex
=
new
IllegalArgumentException
(
"缺少参数"
+
((
MissingServletRequestParameterException
)
ex
).
getParameterName
());
log
.
error
(
"handleException pathInfo:{},param:{}"
,
pathInfo
,
JSON
.
toJSONString
(
request
.
getParameterMap
()),
ex
);
}
else
{
log
.
error
(
"error on handle request: {}"
,
pathInfo
,
ex
);
}
AjaxResult
result
=
new
AjaxResult
();
result
.
setCodeInt
(
monitorException
(
ex
));
if
(
ex
instanceof
IllegalArgumentException
)
{
result
.
setMsg
(
ex
.
getMessage
());
}
else
if
(
ex
instanceof
ServiceException
)
{
result
.
setMsg
(((
ServiceException
)
ex
).
getMsg
());
}
else
if
(
ex
instanceof
MethodArgumentNotValidException
)
{
// controller参数校验错误
StringBuilder
builder
=
new
StringBuilder
();
for
(
FieldError
error:
((
MethodArgumentNotValidException
)
ex
).
getBindingResult
().
getFieldErrors
())
{
builder
.
append
(
error
.
getDefaultMessage
()).
append
(
"."
);
}
result
.
setMsg
(
builder
.
toString
());
}
else
{
int
index
=
ex
.
getMessage
().
indexOf
(
":"
);
if
(
index
!=
-
1
)
{
result
.
setMsg
(
ex
.
getMessage
().
substring
(
index
+
1
));
}
else
{
result
.
setMsg
(
ex
.
getMessage
());
}
}
String
text
=
JSON
.
toJSONString
(
result
);
String
callback
=
request
.
getParameter
(
"callback"
);
if
(!
StringUtils
.
isEmpty
(
callback
))
{
text
=
callback
+
"("
+
text
+
")"
;
}
ResponseUtil
.
writeJson
(
response
,
text
);
}
private
int
monitorException
(
Exception
ex
)
{
if
(
ex
instanceof
IOException
)
{
return
HttpStatus
.
BAD_REQUEST
.
value
();
}
else
if
(
ex
instanceof
IllegalArgumentException
)
{
return
HttpStatus
.
BAD_REQUEST
.
value
();
}
else
if
(
ex
instanceof
ServiceException
)
{
return
((
ServiceException
)
ex
).
getCode
().
getCode
();
}
else
{
return
HttpStatus
.
INTERNAL_SERVER_ERROR
.
value
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment