Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
schbrain-parent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
framework
schbrain-parent
Commits
a9e19048
Commit
a9e19048
authored
Jun 01, 2023
by
liaozan
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve exceptionHanding
parent
e1f5d54c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
89 additions
and
22 deletions
+89
-22
commons/web-common/src/main/java/com/schbrain/common/web/ExceptionHandingConfiguration.java
...om/schbrain/common/web/ExceptionHandingConfiguration.java
+12
-2
commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultExceptionTranslator.java
...rain/common/web/exception/DefaultExceptionTranslator.java
+31
-0
commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionHandler.java
...n/common/web/exception/DefaultGlobalExceptionHandler.java
+23
-20
commons/web-common/src/main/java/com/schbrain/common/web/exception/ExceptionTranslator.java
...om/schbrain/common/web/exception/ExceptionTranslator.java
+23
-0
No files found.
commons/web-common/src/main/java/com/schbrain/common/web/ExceptionHandingConfiguration.java
View file @
a9e19048
...
...
@@ -2,11 +2,14 @@ package com.schbrain.common.web;
import
com.schbrain.common.web.exception.*
;
import
com.schbrain.common.web.properties.WebProperties
;
import
org.springframework.beans.factory.ObjectProvider
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
java.util.stream.Collectors
;
/**
* @author liaozan
* @since 2023-05-08
...
...
@@ -16,8 +19,15 @@ public class ExceptionHandingConfiguration {
@Bean
@ConditionalOnMissingBean
public
GlobalExceptionHandler
defaultGlobalExceptionHandler
()
{
return
new
DefaultGlobalExceptionHandler
();
public
GlobalExceptionHandler
defaultGlobalExceptionHandler
(
ObjectProvider
<
ExceptionTranslator
>
exceptionTranslators
)
{
return
new
DefaultGlobalExceptionHandler
(
exceptionTranslators
.
orderedStream
().
collect
(
Collectors
.
toList
()));
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean
(
GlobalExceptionHandler
.
class
)
public
ExceptionTranslator
defaultExceptionTranslator
()
{
return
new
DefaultExceptionTranslator
();
}
@Bean
...
...
commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultExceptionTranslator.java
0 → 100644
View file @
a9e19048
package
com.schbrain.common.web.exception
;
import
com.schbrain.common.exception.BaseException
;
import
com.schbrain.common.util.EnvUtils
;
import
com.schbrain.common.web.result.ResponseDTO
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.core.Ordered
;
/**
* @author liaozan
* @since 2023-06-01
*/
public
class
DefaultExceptionTranslator
implements
ExceptionTranslator
{
@Override
public
ResponseDTO
<
String
>
translate
(
Throwable
throwable
,
int
code
,
int
action
,
String
message
)
{
if
(
throwable
instanceof
BaseException
)
{
return
ResponseDTO
.
error
((
BaseException
)
throwable
);
}
if
(
EnvUtils
.
isProduction
()
||
StringUtils
.
isBlank
(
message
))
{
return
ResponseDTO
.
error
(
"系统错误"
,
code
);
}
return
ResponseDTO
.
error
(
message
,
code
,
action
);
}
@Override
public
int
getOrder
()
{
return
Ordered
.
LOWEST_PRECEDENCE
;
}
}
\ No newline at end of file
commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionHandler.java
View file @
a9e19048
...
...
@@ -2,11 +2,10 @@ package com.schbrain.common.web.exception;
import
cn.hutool.core.exceptions.ExceptionUtil
;
import
cn.hutool.core.util.StrUtil
;
import
com.schbrain.common.constants.ResponseActionConstants
;
import
com.schbrain.common.exception.BaseException
;
import
com.schbrain.common.util.EnvUtils
;
import
com.schbrain.common.web.result.ResponseDTO
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.hibernate.validator.internal.engine.path.PathImpl
;
import
org.springframework.dao.DataAccessException
;
import
org.springframework.http.HttpStatus
;
...
...
@@ -37,11 +36,17 @@ import static com.schbrain.common.constants.ResponseCodeConstants.*;
@ResponseStatus
(
HttpStatus
.
OK
)
public
class
DefaultGlobalExceptionHandler
implements
GlobalExceptionHandler
{
private
final
List
<
ExceptionTranslator
>
exceptionTranslators
;
public
DefaultGlobalExceptionHandler
(
List
<
ExceptionTranslator
>
exceptionTranslators
)
{
this
.
exceptionTranslators
=
exceptionTranslators
;
}
/************************************* Base Exception Handing *************************************/
@ExceptionHandler
(
BaseException
.
class
)
public
ResponseDTO
<
String
>
handleBaseException
(
BaseException
ex
)
{
logError
(
ex
);
return
ResponseDTO
.
error
(
ex
);
return
buildResponse
(
ex
,
ex
.
getCode
(),
ex
.
getAction
(),
ex
.
getMessage
()
);
}
/************************************* Common Exception Handing *************************************/
...
...
@@ -185,25 +190,31 @@ public class DefaultGlobalExceptionHandler implements GlobalExceptionHandler {
return
buildResponse
(
ex
,
PARAM_INVALID
,
errorMsg
);
}
protected
ResponseDTO
<
String
>
loggingThenBuildResponse
(
Throwable
throwable
,
int
errorC
ode
)
{
protected
ResponseDTO
<
String
>
loggingThenBuildResponse
(
Throwable
throwable
,
int
c
ode
)
{
Throwable
rootCause
=
ExceptionUtil
.
getRootCause
(
throwable
);
logError
(
rootCause
);
return
buildResponse
(
rootCause
,
errorC
ode
,
rootCause
.
getMessage
());
return
buildResponse
(
rootCause
,
c
ode
,
rootCause
.
getMessage
());
}
protected
ResponseDTO
<
String
>
buildResponse
(
Throwable
throwable
,
int
code
,
String
message
)
{
boolean
isProduction
=
EnvUtils
.
isProduction
();
ResponseDTO
<
String
>
responseDTO
=
getExceptionResponseMapping
(
throwable
,
isProduction
);
return
buildResponse
(
throwable
,
code
,
ResponseActionConstants
.
ALERT
,
message
);
}
protected
ResponseDTO
<
String
>
buildResponse
(
Throwable
throwable
,
int
code
,
int
action
,
String
message
)
{
ResponseDTO
<
String
>
responseDTO
=
translateException
(
throwable
,
code
,
action
,
message
);
if
(
responseDTO
!=
null
)
{
return
responseDTO
;
}
if
(
isProduction
||
StringUtils
.
isBlank
(
message
))
{
return
ResponseDTO
.
error
(
"系统错误"
,
code
);
}
return
ResponseDTO
.
error
(
message
,
code
);
return
ResponseDTO
.
error
(
message
,
code
,
action
);
}
protected
ResponseDTO
<
String
>
getExceptionResponseMapping
(
Throwable
throwable
,
boolean
isProduction
)
{
protected
ResponseDTO
<
String
>
translateException
(
Throwable
throwable
,
int
code
,
int
action
,
String
message
)
{
for
(
ExceptionTranslator
exceptionTranslator
:
exceptionTranslators
)
{
ResponseDTO
<
String
>
responseDTO
=
exceptionTranslator
.
translate
(
throwable
,
code
,
action
,
message
);
if
(
responseDTO
!=
null
)
{
return
responseDTO
;
}
}
return
null
;
}
...
...
@@ -235,15 +246,7 @@ public class DefaultGlobalExceptionHandler implements GlobalExceptionHandler {
protected
void
logError
(
Throwable
throwable
)
{
String
exMsg
=
ExceptionUtil
.
getMessage
(
throwable
);
if
(
hasCause
(
throwable
))
{
exMsg
=
exMsg
+
", "
+
ExceptionUtil
.
getRootCauseMessage
(
throwable
);
throwable
=
ExceptionUtil
.
getRootCause
(
throwable
);
}
log
.
error
(
exMsg
,
throwable
);
}
private
boolean
hasCause
(
Throwable
throwable
)
{
return
throwable
.
getCause
()
!=
null
;
}
}
\ No newline at end of file
commons/web-common/src/main/java/com/schbrain/common/web/exception/ExceptionTranslator.java
0 → 100644
View file @
a9e19048
package
com.schbrain.common.web.exception
;
import
com.schbrain.common.web.result.ResponseDTO
;
import
org.springframework.core.Ordered
;
/**
* @author liaozan
* @since 2023-06-01
*/
@FunctionalInterface
public
interface
ExceptionTranslator
extends
Ordered
{
/**
* Translate the exception to {@link ResponseDTO}
*/
ResponseDTO
<
String
>
translate
(
Throwable
throwable
,
int
code
,
int
action
,
String
message
);
@Override
default
int
getOrder
()
{
return
Ordered
.
LOWEST_PRECEDENCE
-
100
;
}
}
\ No newline at end of file
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