Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
indicators
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
zhangkb
indicators
Commits
421445c3
Commit
421445c3
authored
Apr 17, 2020
by
张祺
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加基础代码
parent
f173dbb5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
90 additions
and
16 deletions
+90
-16
BaseCodeException.java
...com/keymobile/indicators/exception/BaseCodeException.java
+18
-0
BaseException.java
...ava/com/keymobile/indicators/exception/BaseException.java
+11
-0
RestExceptionHandler.java
.../keymobile/indicators/exception/RestExceptionHandler.java
+53
-16
Result.java
src/main/java/com/keymobile/indicators/result/Result.java
+4
-0
ResultCodeEnum.java
.../java/com/keymobile/indicators/result/ResultCodeEnum.java
+4
-0
No files found.
src/main/java/com/keymobile/indicators/exception/BaseCodeException.java
0 → 100644
View file @
421445c3
package
com
.
keymobile
.
indicators
.
exception
;
import
com.keymobile.indicators.result.ResultCodeEnum
;
import
lombok.Data
;
@Data
public
class
BaseCodeException
extends
Exception
{
/**
* 异常编码
*/
private
ResultCodeEnum
errCode
;
public
BaseCodeException
(
ResultCodeEnum
code
,
String
message
)
{
super
(
message
);
this
.
errCode
=
code
;
}
}
src/main/java/com/keymobile/indicators/exception/BaseException.java
0 → 100644
View file @
421445c3
package
com
.
keymobile
.
indicators
.
exception
;
/**
* 基础异常
*/
public
class
BaseException
extends
Exception
{
public
BaseException
(
String
message
)
{
super
(
message
);
}
}
src/main/java/com/keymobile/indicators/exception/RestExceptionHandler.java
View file @
421445c3
package
com
.
keymobile
.
indicators
.
exception
;
import
com.keymobile.indicators.result.Result
;
import
com.keymobile.indicators.result.ResultCodeEnum
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.core.Ordered
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.dao.DataIntegrityViolationException
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.validation.BindException
;
import
org.springframework.validation.FieldError
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.context.request.WebRequest
;
import
org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
;
import
static
org
.
springframework
.
http
.
HttpStatus
.
INTERNAL_SERVER_ERROR
;
import
javax.naming.AuthenticationException
;
@Order
(
Ordered
.
HIGHEST_PRECEDENCE
)
@ControllerAdvice
public
class
RestExceptionHandler
extends
ResponseEntityExceptionHandler
{
@Slf4j
public
class
RestExceptionHandler
{
@ExceptionHandler
(
Exception
.
class
)
protected
ResponseEntity
<
Object
>
handlException
(
Exception
ex
,
WebRequest
request
)
{
ApiError
apiError
;
if
(
ex
instanceof
DataIntegrityViolationException
)
{
apiError
=
new
ApiError
(
INTERNAL_SERVER_ERROR
,
ex
.
getMessage
(),
"重复值"
,
ex
);
}
else
{
apiError
=
new
ApiError
(
INTERNAL_SERVER_ERROR
,
ex
.
getMessage
(),
ex
);
protected
Result
<
Object
>
handlException
(
Exception
ex
)
{
Throwable
e
=
ex
.
getCause
();
if
(
e
==
null
)
{
e
=
ex
;
}
return
buildResponseEntity
(
apiError
);
}
if
(
e
instanceof
BindException
)
{
FieldError
fe
=
((
BindException
)
e
).
getBindingResult
().
getFieldError
();
String
errorMsg
=
fe
.
getField
()
+
fe
.
getDefaultMessage
();
log
.
warn
(
errorMsg
,
e
);
return
Result
.
genOteherCodeFailResult
(
ResultCodeEnum
.
VALIDATE_ERROR_CODE
,
errorMsg
);
}
if
(
e
instanceof
AuthenticationException
){
String
errorMsg
=
e
.
getMessage
();
log
.
warn
(
errorMsg
,
e
);
return
Result
.
genOteherCodeFailResult
(
ResultCodeEnum
.
VALIDATE_ERROR_CODE
,
errorMsg
);
}
if
(
e
instanceof
IllegalArgumentException
)
{
String
errorMsg
=
e
.
getMessage
();
log
.
warn
(
errorMsg
,
e
);
return
Result
.
genOteherCodeFailResult
(
ResultCodeEnum
.
VALIDATE_ERROR_CODE
,
errorMsg
);
}
if
(
e
instanceof
MethodArgumentNotValidException
)
{
FieldError
fe
=
((
MethodArgumentNotValidException
)
e
).
getBindingResult
().
getFieldError
();
String
errorMsg
=
fe
.
getField
()
+
fe
.
getDefaultMessage
();
log
.
info
(
errorMsg
,
e
);
return
Result
.
genOteherCodeFailResult
(
ResultCodeEnum
.
VALIDATE_ERROR_CODE
,
errorMsg
);
private
ResponseEntity
<
Object
>
buildResponseEntity
(
ApiError
apiError
)
{
return
new
ResponseEntity
<>(
apiError
,
apiError
.
getStatus
());
}
if
(
e
instanceof
BaseCodeException
)
{
ResultCodeEnum
errcode
=
((
BaseCodeException
)
e
).
getErrCode
();
String
errorMsg
=
e
.
getMessage
();
log
.
info
(
errorMsg
,
e
);
return
Result
.
genOteherCodeFailResult
(
errcode
,
errorMsg
);
}
if
(
e
instanceof
BaseException
)
{
String
errorMsg
=
e
.
getMessage
();
log
.
info
(
errorMsg
,
e
);
return
Result
.
genOteherCodeFailResult
(
ResultCodeEnum
.
VALIDATE_ERROR_CODE
,
errorMsg
);
}
log
.
error
(
e
.
getMessage
(),
e
);
return
Result
.
genOteherCodeFailResult
(
ResultCodeEnum
.
BASE_ERROR_CODE
,
ResultCodeEnum
.
BASE_ERROR_CODE
.
getMsg
());
}
}
src/main/java/com/keymobile/indicators/result/Result.java
View file @
421445c3
...
...
@@ -90,6 +90,10 @@ public class Result<T> implements Serializable {
return
new
Result
<
Object
>(
resultCodeEnum
,
data
);
}
public
static
Result
genOteherCodeFailResult
(
ResultCodeEnum
resultCodeEnum
,
final
String
msg
)
{
return
new
Result
<
Object
>(
resultCodeEnum
,
msg
,
null
);
}
public
static
Result
genMethodErrorResult
()
{
return
new
Result
<
Object
>(
ResultCodeEnum
.
METHOD_NOT_ALLOWED
,
DEFAULT_METHOD_NOT_ALLOWED_MESSAGE
);
}
...
...
src/main/java/com/keymobile/indicators/result/ResultCodeEnum.java
View file @
421445c3
...
...
@@ -5,6 +5,7 @@ package com.keymobile.indicators.result;
*/
public
enum
ResultCodeEnum
{
// 响应码定义
// ====== 通用状态码 ======
// 正常
...
...
@@ -25,6 +26,9 @@ public enum ResultCodeEnum {
INVALID_TYPE
(-
5
,
"%s 参数类型不正确 value:%s"
),
INVALID_PARAM
(-
5
,
"%s 参数不正确 value:%s"
),
INVALID_BODY_PARAM
(-
5
,
"请求体不正确:%s"
),
VALIDATE_ERROR_CODE
(-
6
,
"验证错误"
),
BASE_ERROR_CODE
(-
7
,
"基础错误"
),
OCCUPATION_CLOSED
(
10001
,
"所查占用已被临时关闭"
),
...
...
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