Skip to content

Commit

Permalink
fix: Updated logging
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-gryzbon committed Jun 12, 2022
1 parent 8f6e303 commit 7e1f222
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class ClientErrorDecoder : ErrorDecoder {
val status = response.status()
val responseBody = getResponseBody(response)
log().error("Service returned status:{}, content:{}", status, responseBody)
return getException(status, responseBody)
val exception = getException(status, responseBody)
log().debug("Generated exception:{}", exception?.message, exception)
return exception
}

private fun getResponseBody(response: Response) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ class ControllerExceptionHandler(
@ExceptionHandler(Exception::class)
@ResponseBody
protected fun handleUncaughtException(ex: Exception, request: WebRequest): ResponseEntity<*> {
return when (ex) {
val responseEntity = when (ex) {
is ConstraintViolationException -> handle(ex, request)
is ResponseStatusException -> handle(ex, request)
is ErrorResponseException -> handle(ex, request)
else -> ResponseEntity(unknownErrorHandler.handle(ex, request), getResponseStatusFromAnnotation(ex))
}
log().error("For request:{}, generated httpStatus:{}, errorResponse:{}", request, responseEntity.statusCode, responseEntity.body)
log().error("Handling exception", ex)
return responseEntity
}

private fun handle(ex: ErrorResponseException, request: WebRequest): ResponseEntity<*> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class CustomResponseEntityExceptionHandler(
messages = listOf(VALIDATION_FAILED_MESSAGE),
dataErrors = listOf(dataError),
attributes = attributePopulator.populateAttributes(ex, request))
log().error("Handling MissingServletRequestParameterException - httpStatus:{}, errorResponse:{}", status, errorResponse)
return ResponseEntity(errorResponse, status)
}

Expand All @@ -73,6 +74,7 @@ class CustomResponseEntityExceptionHandler(
messages = listOf(DATA_MISSING_MESSAGE),
dataErrors = dataErrorsFromBindingResults(ex.bindingResult),
attributes = attributePopulator.populateAttributes(ex, request))
log().error("Handling BindException - httpStatus:{}, errorResponse:{}", status, errorResponse)
return ResponseEntity(errorResponse, status)
}

Expand All @@ -87,6 +89,7 @@ class CustomResponseEntityExceptionHandler(
messages = listOf(VALIDATION_FAILED_MESSAGE),
dataErrors = dataErrorsFromBindingResults(ex.bindingResult),
attributes = attributePopulator.populateAttributes(ex, request))
log().error("Handling MethodArgumentNotValidException - httpStatus:{}, errorResponse:{}", status, errorResponse)
return ResponseEntity(errorResponse, status)
}

Expand All @@ -100,6 +103,7 @@ class CustomResponseEntityExceptionHandler(
errorCode = MALFORMED_ERROR_CODE,
messages = listOf(MESSAGE_PARSE_ERROR_MESSAGE),
attributes = attributePopulator.populateAttributes(ex, request))
log().error("Handling HttpMessageNotReadableException - httpStatus:{}, errorResponse:{}", status, errorResponse)
return ResponseEntity(errorResponse, status)
}

Expand All @@ -119,6 +123,7 @@ class CustomResponseEntityExceptionHandler(
messages = listOf(DATA_TYPE_ERROR_MESSAGE),
dataErrors = listOf(dataError),
attributes = attributePopulator.populateAttributes(ex, request))
log().error("Handling TypeMismatchException - httpStatus:{}, errorResponse:{}", status, errorResponse)
return ResponseEntity(errorResponse, status)
}

Expand All @@ -133,7 +138,9 @@ class CustomResponseEntityExceptionHandler(
if (INTERNAL_SERVER_ERROR == status) {
request.setAttribute(ERROR_EXCEPTION_ATTRIBUTE, ex, SCOPE_REQUEST)
}
return ResponseEntity(unknownErrorHandler.handle(ex, request), status)
val errorResponse = unknownErrorHandler.handle(ex, request)
log().error("Handling unknown exception - httpStatus:{}, errorResponse:{}", status, errorResponse)
return ResponseEntity(errorResponse, status)
}

private fun dataErrorsFromBindingResults(bindingResult: BindingResult): List<DataError> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lsdconsulting.exceptionhandling.server.config.attribute

import com.lsdconsulting.exceptionhandling.server.config.log
import com.lsdconsulting.exceptionhandling.server.tracer.RequestTracer
import org.apache.commons.lang3.StringUtils
import org.springframework.core.NestedRuntimeException
Expand Down Expand Up @@ -27,6 +28,7 @@ class AttributePopulator(
START_TIME_ATTRIBUTE, value
)
}
log().error("Populated error response attributes:{}", attributes)
return attributes
}

Expand Down

0 comments on commit 7e1f222

Please sign in to comment.