Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
cloud-platform
Project
Project
Details
Activity
Releases
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
youjj
cloud-platform
Commits
382fb61b
Commit
382fb61b
authored
Aug 19, 2019
by
jiaorz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
全局异常
parent
604e0d90
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
20 deletions
+36
-20
GlobalBasicConfiguration.java
...curity/common/config/global/GlobalBasicConfiguration.java
+6
-0
GlobalExceptionHandler.java
...iaoqi/security/common/handler/GlobalExceptionHandler.java
+6
-20
PlatformExceptionHandler.java
...oqi/security/common/handler/PlatformExceptionHandler.java
+24
-0
No files found.
ace-common/src/main/java/com/github/wxiaoqi/security/common/config/global/GlobalBasicConfiguration.java
View file @
382fb61b
package
com
.
github
.
wxiaoqi
.
security
.
common
.
config
.
global
;
package
com
.
github
.
wxiaoqi
.
security
.
common
.
config
.
global
;
import
com.github.wxiaoqi.security.common.handler.GlobalExceptionHandler
;
import
com.github.wxiaoqi.security.common.handler.GlobalExceptionHandler
;
import
com.github.wxiaoqi.security.common.handler.PlatformExceptionHandler
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
@@ -21,6 +22,11 @@ public class GlobalBasicConfiguration implements ApplicationRunner{
...
@@ -21,6 +22,11 @@ public class GlobalBasicConfiguration implements ApplicationRunner{
private
GlobalExceptionHandler
addGlobalExceptionHandler
()
{
private
GlobalExceptionHandler
addGlobalExceptionHandler
()
{
return
new
GlobalExceptionHandler
();
return
new
GlobalExceptionHandler
();
}
}
@Bean
@ConditionalOnMissingBean
private
PlatformExceptionHandler
addPlatformExceptionHandler
()
{
return
new
PlatformExceptionHandler
();
}
@Autowired
@Autowired
private
void
setEnv
(
Environment
env
)
{
private
void
setEnv
(
Environment
env
)
{
...
...
ace-common/src/main/java/com/github/wxiaoqi/security/common/handler/GlobalExceptionHandler.java
View file @
382fb61b
...
@@ -6,7 +6,6 @@ import com.github.wxiaoqi.security.common.exception.auth.ClientTokenException;
...
@@ -6,7 +6,6 @@ import com.github.wxiaoqi.security.common.exception.auth.ClientTokenException;
import
com.github.wxiaoqi.security.common.exception.auth.UserInvalidException
;
import
com.github.wxiaoqi.security.common.exception.auth.UserInvalidException
;
import
com.github.wxiaoqi.security.common.exception.auth.UserTokenException
;
import
com.github.wxiaoqi.security.common.exception.auth.UserTokenException
;
import
com.github.wxiaoqi.security.common.msg.BaseResponse
;
import
com.github.wxiaoqi.security.common.msg.BaseResponse
;
import
com.github.wxiaoqi.security.common.msg.ObjectRestResponse
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
...
@@ -18,7 +17,7 @@ import javax.servlet.http.HttpServletResponse;
...
@@ -18,7 +17,7 @@ import javax.servlet.http.HttpServletResponse;
/**
/**
* Created by ace on 2017/9/8.
* Created by ace on 2017/9/8.
*/
*/
@ControllerAdvice
@ControllerAdvice
(
"com.github.wxiaoqi.security"
)
@ResponseBody
@ResponseBody
public
class
GlobalExceptionHandler
{
public
class
GlobalExceptionHandler
{
private
Logger
logger
=
LoggerFactory
.
getLogger
(
GlobalExceptionHandler
.
class
);
private
Logger
logger
=
LoggerFactory
.
getLogger
(
GlobalExceptionHandler
.
class
);
...
@@ -48,29 +47,16 @@ public class GlobalExceptionHandler {
...
@@ -48,29 +47,16 @@ public class GlobalExceptionHandler {
public
BaseResponse
baseExceptionHandler
(
HttpServletResponse
response
,
BaseException
ex
)
{
public
BaseResponse
baseExceptionHandler
(
HttpServletResponse
response
,
BaseException
ex
)
{
logger
.
error
(
ex
.
getMessage
(),
ex
);
logger
.
error
(
ex
.
getMessage
(),
ex
);
if
(
0
==
ex
.
getStatus
())
{
if
(
0
==
ex
.
getStatus
())
{
response
.
setStatus
(
1001
);
response
.
setStatus
(
500
);
}
}
return
new
BaseResponse
(
ex
.
getStatus
(),
ex
.
getMessage
());
return
new
BaseResponse
(
ex
.
getStatus
(),
ex
.
getMessage
());
}
}
//服务器异常
@ExceptionHandler
(
Exception
.
class
)
@ExceptionHandler
(
Exception
.
class
)
public
ObjectRestResponse
<?>
exceptionHandler
(
Exception
e
){
public
BaseResponse
otherExceptionHandler
(
HttpServletResponse
response
,
Exception
ex
)
{
Throwable
cause
=
e
.
getCause
();
response
.
setStatus
(
500
);
if
(
cause
!=
null
&&
cause
.
toString
().
contains
(
"Exception"
))
{
logger
.
error
(
ex
.
getMessage
(),
ex
);
logger
.
error
(
cause
.
getMessage
(),
e
);
return
new
BaseResponse
(
CommonConstants
.
EX_OTHER_CODE
,
ex
.
getMessage
());
return
assembleResult
(
ObjectRestResponse
.
createFailedResult
(
5000
,
"服务器开小差了,请稍后重试!"
),
"Server exception: "
+
e
.
getMessage
());
}
logger
.
error
(
"Server exception: "
,
e
);
return
assembleResult
(
ObjectRestResponse
.
createFailedResult
(
5000
,
"服务器开小差了,请稍后重试!"
),
"Server exception: "
+
e
.
getMessage
());
}
protected
ObjectRestResponse
<?>
assembleResult
(
ObjectRestResponse
<?>
error
,
String
msg
)
{
logger
.
debug
(
"Exception: "
+
msg
);
return
error
;
}
}
protected
ObjectRestResponse
<?>
assembleResult
(
ObjectRestResponse
<?>
error
,
Throwable
e
)
{
return
assembleResult
(
error
,
e
.
getClass
().
getSimpleName
()
+
": "
+
e
.
getMessage
());
}
}
}
ace-common/src/main/java/com/github/wxiaoqi/security/common/handler/PlatformExceptionHandler.java
View file @
382fb61b
...
@@ -3,10 +3,13 @@ package com.github.wxiaoqi.security.common.handler;
...
@@ -3,10 +3,13 @@ package com.github.wxiaoqi.security.common.handler;
import
com.github.wxiaoqi.security.common.exception.BaseException
;
import
com.github.wxiaoqi.security.common.exception.BaseException
;
import
com.github.wxiaoqi.security.common.msg.BaseResponse
;
import
com.github.wxiaoqi.security.common.msg.BaseResponse
;
import
com.github.wxiaoqi.security.common.msg.ObjectRestResponse
;
import
lombok.extern.slf4j.Slf4j
;
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
;
@RestControllerAdvice
(
"com.xxfc.platform"
)
@RestControllerAdvice
(
"com.xxfc.platform"
)
@Slf4j
public
class
PlatformExceptionHandler
{
public
class
PlatformExceptionHandler
{
@ExceptionHandler
(
value
=
{
BaseException
.
class
})
@ExceptionHandler
(
value
=
{
BaseException
.
class
})
...
@@ -18,4 +21,25 @@ public class PlatformExceptionHandler {
...
@@ -18,4 +21,25 @@ public class PlatformExceptionHandler {
return
new
BaseResponse
(
400
,
e
.
getMessage
());
return
new
BaseResponse
(
400
,
e
.
getMessage
());
}
}
//服务器异常
@ExceptionHandler
(
Exception
.
class
)
public
ObjectRestResponse
<?>
exceptionHandler
(
Exception
e
){
Throwable
cause
=
e
.
getCause
();
if
(
cause
!=
null
&&
cause
.
toString
().
contains
(
"Exception"
))
{
log
.
error
(
cause
.
getMessage
(),
e
);
return
assembleResult
(
ObjectRestResponse
.
createFailedResult
(
5000
,
"服务器开小差了,请稍后重试!"
),
"Server exception: "
+
e
.
getMessage
());
}
log
.
error
(
"Server exception: "
,
e
);
return
assembleResult
(
ObjectRestResponse
.
createFailedResult
(
5000
,
"服务器开小差了,请稍后重试!"
),
"Server exception: "
+
e
.
getMessage
());
}
protected
ObjectRestResponse
<?>
assembleResult
(
ObjectRestResponse
<?>
error
,
String
msg
)
{
log
.
debug
(
"Exception: "
+
msg
);
return
error
;
}
protected
ObjectRestResponse
<?>
assembleResult
(
ObjectRestResponse
<?>
error
,
Throwable
e
)
{
return
assembleResult
(
error
,
e
.
getClass
().
getSimpleName
()
+
": "
+
e
.
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