diff --git a/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/common/errorhandling/ExceptionHandler.java b/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/common/errorhandling/ExceptionHandler.java index 11318a0cf33d16cc95296cef55b61c1f08a878b8..27b693e6735817c0e7b69c0e9f3deb978cbb89c8 100644 --- a/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/common/errorhandling/ExceptionHandler.java +++ b/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/common/errorhandling/ExceptionHandler.java @@ -102,6 +102,16 @@ public class ExceptionHandler { return Status.INTERNAL.withDescription(message).withCause(e.getCause()); } + @GrpcExceptionHandler + public StatusException handleNotFoundException(de.ozgcloud.apilib.common.errorhandling.NotFoundException e) { + LOG.error(ERROR_MESSAGE, e); + var exceptionId = createExceptionId(); + var messageWithExceptionId = ExceptionUtil.formatMessageWithExceptionId(e.getMessage(), exceptionId); + var status = Status.NOT_FOUND.withDescription(messageWithExceptionId).withCause(e.getCause()); + + return createStatusException(status, buildMetadata(exceptionId)); + } + private Metadata buildMetadata(String exceptionId) { var metadata = new Metadata(); addExceptionId(metadata, exceptionId); diff --git a/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/common/errorhandling/ExceptionHandlerTest.java b/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/common/errorhandling/ExceptionHandlerTest.java index 5988d1bde47af660ad42c7ee6d613ad9aff7c3f5..5387ed1efef766385e8a769883af4c31c8c30189 100644 --- a/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/common/errorhandling/ExceptionHandlerTest.java +++ b/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/common/errorhandling/ExceptionHandlerTest.java @@ -29,12 +29,14 @@ import static org.mockito.Mockito.*; import java.util.Map; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Spy; import org.springframework.security.access.AccessDeniedException; +import de.ozgcloud.apilib.common.datatypes.GenericId; import de.ozgcloud.common.errorhandling.FunctionalErrorCode; import de.ozgcloud.common.errorhandling.TechnicalException; import de.ozgcloud.vorgang.vorgang.Vorgang; @@ -308,4 +310,55 @@ class ExceptionHandlerTest { return handler.handleSearchServiceUnavailableException(exception); } } + + @DisplayName("Handle not found exception (from api lib)") + @Nested + class TestHandleNotFoundExceptionFromApiLib { + + private final String exceptionId = "42"; + private final GenericId id = GenericId.from(VorgangTestFactory.ID); + private final String entityName = Vorgang.class.toString(); + + @BeforeEach + void mockExceptionId() { + doReturn(exceptionId).when(handler).createExceptionId(); + } + + @Test + void shouldHaveStatusCode() { + var status = handleException().getStatus(); + + assertThat(status.getCode()).isEqualTo(Code.NOT_FOUND); + } + + @Test + void shouldHaveMessage() { + var statusException = handleException(); + + assertThat(statusException.getMessage()).contains(id.toString()); + assertThat(statusException.getMessage()).contains(entityName); + assertThat(statusException.getMessage()).contains(exceptionId); + } + + @Test + void shouldHaveStatusDescription() { + var status = handleException().getStatus(); + + assertThat(status.getDescription()).contains(id.toString()); + assertThat(status.getDescription()).contains(entityName); + assertThat(status.getDescription()).contains(exceptionId); + } + + @Test + void shouldHaveExceptionId() { + var metaData = handleException().getTrailers(); + + assertThat(metaData.get(createExceptionIdKey())).isEqualTo(exceptionId); + } + + private StatusException handleException() { + return handler.handleNotFoundException( + new de.ozgcloud.apilib.common.errorhandling.NotFoundException(id, entityName)); + } + } } \ No newline at end of file