Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Solidify-Backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Solidify
Solidify-Backend
Commits
5e95e282
Commit
5e95e282
authored
1 year ago
by
Hugues.Cazeaux
Browse files
Options
Downloads
Patches
Plain Diff
feat(SolidifyControllerAdvice): add detailed information for exception error
parent
3d06314e
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!424
feat(SolidifyControllerAdvice): add detailed information for exception error
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
solidify-controller/src/main/java/ch/unige/solidify/controller/SolidifyControllerAdvice.java
+16
-12
16 additions, 12 deletions
...h/unige/solidify/controller/SolidifyControllerAdvice.java
with
16 additions
and
12 deletions
solidify-controller/src/main/java/ch/unige/solidify/controller/SolidifyControllerAdvice.java
+
16
−
12
View file @
5e95e282
...
...
@@ -28,6 +28,9 @@ import static ch.unige.solidify.controller.SolidifyControllerAdvice.StackTraceDi
import
java.util.NoSuchElementException
;
import
jakarta.servlet.ServletException
;
import
jakarta.servlet.http.HttpServletRequest
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.boot.web.servlet.error.ErrorAttributes
;
...
...
@@ -58,8 +61,6 @@ import org.springframework.web.servlet.resource.NoResourceFoundException;
import
com.fasterxml.jackson.core.JsonParseException
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
jakarta.servlet.ServletException
;
import
jakarta.servlet.http.HttpServletRequest
;
import
ch.unige.solidify.config.SolidifyProperties
;
import
ch.unige.solidify.exception.SolidifyBulkActionException
;
...
...
@@ -72,6 +73,7 @@ import ch.unige.solidify.exception.SolidifyUndeletableException;
import
ch.unige.solidify.exception.SolidifyUnmodifiableException
;
import
ch.unige.solidify.exception.SolidifyValidationException
;
import
ch.unige.solidify.service.MessageService
;
import
ch.unige.solidify.util.StringTool
;
import
ch.unige.solidify.validation.ValidationError
;
@ControllerAdvice
...
...
@@ -147,7 +149,7 @@ public class SolidifyControllerAdvice {
@ExceptionHandler
(
Exception
.
class
)
public
ResponseEntity
<
SolidifyError
>
handleException
(
Exception
exception
,
HttpServletRequest
request
)
{
this
.
logException
(
exception
,
request
,
WITH_STACK_TRACE
);
this
.
logException
(
exception
,
null
,
request
,
WITH_STACK_TRACE
);
final
SolidifyError
error
=
new
SolidifyError
(
this
.
errorAttributes
,
request
);
error
.
setMessage
(
exception
.
getMessage
());
return
ResponseEntity
.
status
(
error
.
getStatus
()).
headers
(
this
.
headers
).
body
(
error
);
...
...
@@ -163,7 +165,7 @@ public class SolidifyControllerAdvice {
@ExceptionHandler
(
HttpMessageNotReadableException
.
class
)
public
ResponseEntity
<
SolidifyError
>
handleException
(
HttpMessageNotReadableException
exception
,
HttpServletRequest
request
)
{
this
.
logException
(
exception
,
request
,
WITH_STACK_TRACE
);
this
.
logException
(
exception
,
null
,
request
,
WITH_STACK_TRACE
);
String
message
=
this
.
messageService
.
get
(
"error.request.notreadable"
);
...
...
@@ -242,7 +244,7 @@ public class SolidifyControllerAdvice {
@ExceptionHandler
(
SolidifyRestException
.
class
)
public
ResponseEntity
<
SolidifyError
>
handleHttpRestClientException
(
SolidifyRestException
exception
,
HttpServletRequest
request
)
{
this
.
logException
(
exception
,
request
,
WITH_STACK_TRACE
);
this
.
logException
(
exception
,
null
,
request
,
WITH_STACK_TRACE
);
SolidifyError
error
=
exception
.
getSourceError
();
if
(
error
==
null
)
{
error
=
new
SolidifyError
(
this
.
errorAttributes
,
request
);
...
...
@@ -297,7 +299,7 @@ public class SolidifyControllerAdvice {
}
protected
ResponseEntity
<
SolidifyError
>
buildBadRequestResponse
(
Exception
exception
,
HttpServletRequest
request
,
SolidifyError
solidifyError
)
{
this
.
logException
(
exception
,
request
,
this
.
badRequestStacktrace
);
this
.
logException
(
exception
,
null
,
request
,
this
.
badRequestStacktrace
);
this
.
logErrorAsJson
(
solidifyError
);
return
ResponseEntity
.
badRequest
().
headers
(
this
.
headers
).
body
(
solidifyError
);
}
...
...
@@ -315,7 +317,7 @@ public class SolidifyControllerAdvice {
protected
ResponseEntity
<
SolidifyError
>
buildResponse
(
Exception
exception
,
HttpServletRequest
request
,
String
technicalMessage
,
HttpStatusCode
httpStatusCode
,
StackTraceDisplay
withStackTrace
)
{
this
.
logException
(
exception
,
request
,
withStackTrace
);
this
.
logException
(
exception
,
technicalMessage
,
request
,
withStackTrace
);
final
SolidifyError
error
=
new
SolidifyError
(
this
.
errorAttributes
,
request
);
error
.
setMessage
(
exception
.
getMessage
());
// Message for user (i18n)
...
...
@@ -334,14 +336,16 @@ public class SolidifyControllerAdvice {
}
}
protected
void
logException
(
Exception
exception
,
HttpServletRequest
request
,
StackTraceDisplay
stackTraceDisplay
)
{
protected
void
logException
(
Exception
exception
,
String
technicalMessage
,
HttpServletRequest
request
,
StackTraceDisplay
stackTraceDisplay
)
{
final
String
headerXFF
=
request
.
getHeader
(
"X-FORWARDED-FOR"
);
String
message
=
"Request url {} {} from remote address {} with X-FORWARDED-FOR {} has generated an exception caught by controller advice:"
;
if
(
stackTraceDisplay
==
WITH_STACK_TRACE
)
{
log
.
error
(
message
,
request
.
getMethod
(),
request
.
getRequestURL
(),
request
.
getRemoteAddr
(),
headerXFF
,
exception
);
}
else
if
(
stackTraceDisplay
==
WITHOUT_STACK_TRACE
)
{
if
(
stackTraceDisplay
==
WITHOUT_STACK_TRACE
)
{
message
+=
" {}"
;
log
.
info
(
message
,
request
.
getMethod
(),
request
.
getRequestURL
(),
request
.
getRemoteAddr
(),
headerXFF
,
exception
);
}
log
.
error
(
message
,
request
.
getMethod
(),
request
.
getRequestURL
(),
request
.
getRemoteAddr
(),
headerXFF
,
exception
);
// Log technical information
if
(!
StringTool
.
isNullOrEmpty
(
technicalMessage
))
{
log
.
error
(
"Exception error details: {}"
,
technicalMessage
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment