Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gic-webapp-plug
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
base_platform_enterprise
gic-webapp-plug
Commits
038f3f1e
Commit
038f3f1e
authored
May 27, 2021
by
xuwenqian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:异常修改
parent
17752986
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
20 deletions
+21
-20
SpringGlobalExceptionHandler.java
.../gic/plug/web/exception/SpringGlobalExceptionHandler.java
+21
-20
No files found.
src/main/java/com/gic/plug/web/exception/SpringGlobalExceptionHandler.java
View file @
038f3f1e
...
@@ -3,6 +3,7 @@ package com.gic.plug.web.exception;
...
@@ -3,6 +3,7 @@ package com.gic.plug.web.exception;
import
com.gic.commons.exception.BaseRuntimeException
;
import
com.gic.commons.exception.BaseRuntimeException
;
import
com.gic.commons.webapi.reponse.RestResponse
;
import
com.gic.commons.webapi.reponse.RestResponse
;
import
com.gic.enterprise.exception.CommonException
;
import
com.gic.enterprise.exception.CommonException
;
import
com.gic.goods.api.dto.ErrorCode
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
...
@@ -12,7 +13,6 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
...
@@ -12,7 +13,6 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.validation.ConstraintViolation
;
import
javax.validation.ConstraintViolation
;
import
javax.validation.ConstraintViolationException
;
import
javax.validation.ConstraintViolationException
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
...
@@ -31,15 +31,26 @@ public class SpringGlobalExceptionHandler {
...
@@ -31,15 +31,26 @@ public class SpringGlobalExceptionHandler {
@ExceptionHandler
(
value
=
Exception
.
class
)
@ExceptionHandler
(
value
=
Exception
.
class
)
public
RestResponse
handlerException
(
Exception
e
xception
)
{
public
RestResponse
handlerException
(
Exception
e
)
{
String
message
=
exception
.
getMessage
(
);
log
.
error
(
"系统异常:{}"
,
e
);
return
RestResponse
.
failure
(
com
.
gic
.
goods
.
api
.
dto
.
ErrorCode
.
UNKNOW_ERROR
.
getCode
(),
com
.
gic
.
goods
.
api
.
dto
.
ErrorCode
.
UNKNOW_ERROR
.
getErrorMessage
());
return
RestResponse
.
failure
(
ErrorCode
.
UNKNOW_ERROR
.
getCode
(),
ErrorCode
.
UNKNOW_ERROR
.
getErrorMessage
());
}
}
@ExceptionHandler
(
value
=
NullPointerException
.
class
)
@ExceptionHandler
(
value
=
NullPointerException
.
class
)
public
RestResponse
nullExceptionHandler
(
HttpServletRequest
req
,
NullPointerException
e
)
{
public
RestResponse
nullExceptionHandler
(
NullPointerException
e
)
{
log
.
error
(
"发生空指针异常!原因是:"
,
e
);
log
.
error
(
"空指针异常!原因是:{}"
,
e
);
return
RestResponse
.
failure
(
com
.
gic
.
goods
.
api
.
dto
.
ErrorCode
.
UNKNOW_ERROR
.
getCode
(),
e
.
getMessage
());
return
RestResponse
.
failure
(
ErrorCode
.
NULL_ERROR
.
getCode
(),
e
.
getMessage
());
}
@ExceptionHandler
(
value
=
BaseRuntimeException
.
class
)
public
RestResponse
baseRuntimeException
(
BaseRuntimeException
e
)
{
log
.
error
(
"运行异常!原因是:{}"
,
e
);
return
RestResponse
.
failure
(
e
.
getErrCode
(),
e
.
getMessage
());
}
@ExceptionHandler
(
value
=
CommonException
.
class
)
public
RestResponse
enterpriseCommonException
(
CommonException
e
)
{
return
RestResponse
.
failure
(
e
.
getErrorCode
(),
e
.
getMessage
());
}
}
/**
/**
...
@@ -51,13 +62,13 @@ public class SpringGlobalExceptionHandler {
...
@@ -51,13 +62,13 @@ public class SpringGlobalExceptionHandler {
@ExceptionHandler
(
BindException
.
class
)
@ExceptionHandler
(
BindException
.
class
)
public
RestResponse
handleBindException
(
BindException
ex
)
{
public
RestResponse
handleBindException
(
BindException
ex
)
{
FieldError
fieldError
=
ex
.
getBindingResult
().
getFieldError
();
FieldError
fieldError
=
ex
.
getBindingResult
().
getFieldError
();
return
RestResponse
.
failure
(
com
.
gic
.
enterprise
.
error
.
ErrorCode
.
MISS_PARAMETER
.
getCode
(),
fieldError
.
getDefaultMessage
());
return
RestResponse
.
failure
(
ErrorCode
.
INVALIDATE_PARAM
.
getCode
(),
fieldError
.
getDefaultMessage
());
}
}
@ExceptionHandler
(
value
=
MethodArgumentNotValidException
.
class
)
@ExceptionHandler
(
value
=
MethodArgumentNotValidException
.
class
)
public
RestResponse
handleNotValid
(
MethodArgumentNotValidException
exception
)
{
public
RestResponse
handleNotValid
(
MethodArgumentNotValidException
exception
)
{
String
message
=
exception
.
getBindingResult
().
getFieldError
().
getDefaultMessage
();
String
message
=
exception
.
getBindingResult
().
getFieldError
().
getDefaultMessage
();
return
RestResponse
.
failure
(
com
.
gic
.
goods
.
api
.
dto
.
ErrorCode
.
INVALIDATE_PARAM
.
getCode
(),
message
);
return
RestResponse
.
failure
(
ErrorCode
.
INVALIDATE_PARAM
.
getCode
(),
message
);
}
}
@ExceptionHandler
(
value
=
ConstraintViolationException
.
class
)
@ExceptionHandler
(
value
=
ConstraintViolationException
.
class
)
...
@@ -65,7 +76,7 @@ public class SpringGlobalExceptionHandler {
...
@@ -65,7 +76,7 @@ public class SpringGlobalExceptionHandler {
List
<
String
>
msg
=
new
ArrayList
<>();
List
<
String
>
msg
=
new
ArrayList
<>();
Set
<
ConstraintViolation
<?>>
constraintViolations
=
exception
.
getConstraintViolations
();
Set
<
ConstraintViolation
<?>>
constraintViolations
=
exception
.
getConstraintViolations
();
RestResponse
result
=
new
RestResponse
();
RestResponse
result
=
new
RestResponse
();
result
.
setCode
(
com
.
gic
.
goods
.
api
.
dto
.
ErrorCode
.
INVALIDATE_PARAM
.
getCode
());
result
.
setCode
(
ErrorCode
.
INVALIDATE_PARAM
.
getCode
());
for
(
ConstraintViolation
<?>
constraintViolation
:
constraintViolations
)
{
for
(
ConstraintViolation
<?>
constraintViolation
:
constraintViolations
)
{
msg
.
add
(
constraintViolation
.
getMessage
());
msg
.
add
(
constraintViolation
.
getMessage
());
}
}
...
@@ -73,14 +84,4 @@ public class SpringGlobalExceptionHandler {
...
@@ -73,14 +84,4 @@ public class SpringGlobalExceptionHandler {
return
result
;
return
result
;
}
}
@ExceptionHandler
(
value
=
CommonException
.
class
)
public
RestResponse
enterpriseCommonException
(
CommonException
exception
)
{
return
RestResponse
.
failure
(
exception
.
getErrorCode
(),
exception
.
getMessage
());
}
@ExceptionHandler
(
value
=
BaseRuntimeException
.
class
)
public
RestResponse
baseRuntimeException
(
BaseRuntimeException
exception
)
throws
Exception
{
return
RestResponse
.
failure
(
exception
.
getErrCode
(),
exception
.
getMessage
());
}
}
}
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