generated from hmcts/spring-boot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore LoggingExceptionResolver to capture/monitor/alter exceptions …
…return to Client
- Loading branch information
1 parent
36bee12
commit 93fcad3
Showing
9 changed files
with
88 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/uk/gov/hmcts/opal/controllers/LoggingExceptionResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package uk.gov.hmcts.opal.controllers; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.hibernate.PropertyValueException; | ||
import org.springframework.dao.DataIntegrityViolationException; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerExceptionResolver; | ||
import org.springframework.web.servlet.ModelAndView; | ||
import uk.gov.hmcts.opal.authentication.service.AccessTokenService; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j(topic = "LoggingExceptionResolver") | ||
public class LoggingExceptionResolver implements HandlerExceptionResolver { | ||
|
||
private final AccessTokenService tokenService; | ||
|
||
@Override | ||
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, | ||
Object handler, Exception ex) { | ||
if (ex instanceof DataIntegrityViolationException dive) { | ||
log.warn(":resolveException: DataIntegrityViolationException: ", dive.getCause()); | ||
} else if (ex instanceof PropertyValueException pve) { | ||
log.warn(":resolveException: for Entity '{}', value '{}': {}", | ||
pve.getEntityName(), pve.getPropertyName(), pve.getMessage()); | ||
} else { | ||
log.warn(":resolveException: {}: '{}'", ex.getClass().getSimpleName(), ex.getMessage()); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/test/java/uk/gov/hmcts/opal/controllers/LoggingExceptionResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package uk.gov.hmcts.opal.controllers; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.hibernate.PropertyValueException; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.dao.DataIntegrityViolationException; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class LoggingExceptionResolverTest { | ||
|
||
@InjectMocks | ||
private LoggingExceptionResolver loggingExceptionResolver; | ||
|
||
@Test | ||
void codeCoverage() { | ||
// Arrange | ||
HttpServletRequest request = Mockito.mock(HttpServletRequest.class); | ||
HttpServletResponse response = Mockito.mock(HttpServletResponse.class); | ||
Object object = "null"; | ||
|
||
// Act | ||
loggingExceptionResolver.resolveException(request, response, object, new DataIntegrityViolationException("")); | ||
loggingExceptionResolver.resolveException(request, response, object, new PropertyValueException("", "", "")); | ||
loggingExceptionResolver.resolveException(request, response, object, new Exception()); | ||
} | ||
|
||
} |