Skip to content
Snippets Groups Projects
Commit bb98f9e3 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6858 handle NotFoundException from api lib

parent 5f244a76
No related branches found
No related tags found
No related merge requests found
......@@ -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);
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment