Skip to content
Snippets Groups Projects
Commit 844a1a87 authored by Krzysztof Witukiewicz's avatar Krzysztof Witukiewicz
Browse files

OZG-7262 OZG-7627 Remove BinaryFileDownloadException

parent da68d4a4
No related branches found
No related tags found
1 merge request!6OZG-7262 OZG-7566 Notify callObserver about error
/*
* Copyright (C) 2025 Das Land Schleswig-Holstein vertreten durch den
* Ministerpräsidenten des Landes Schleswig-Holstein
* Staatskanzlei
* Abteilung Digitalisierung und zentrales IT-Management der Landesregierung
*
* Lizenziert unter der EUPL, Version 1.2 oder - sobald
* diese von der Europäischen Kommission genehmigt wurden -
* Folgeversionen der EUPL ("Lizenz");
* Sie dürfen dieses Werk ausschließlich gemäß
* dieser Lizenz nutzen.
* Eine Kopie der Lizenz finden Sie hier:
*
* https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* Sofern nicht durch anwendbare Rechtsvorschriften
* gefordert oder in schriftlicher Form vereinbart, wird
* die unter der Lizenz verbreitete Software "so wie sie
* ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
* ausdrücklich oder stillschweigend - verbreitet.
* Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen.
*/
package de.ozgcloud.common.binaryfile;
import de.ozgcloud.common.errorhandling.TechnicalException;
class BinaryFileDownloadException extends TechnicalException {
public BinaryFileDownloadException(Throwable cause) {
super("Error occurred during downloading file content", cause);
}
}
......@@ -27,6 +27,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
......@@ -37,6 +38,7 @@ import org.springframework.core.task.TaskExecutor;
import com.google.protobuf.ByteString;
import de.ozgcloud.common.errorhandling.TechnicalException;
import io.grpc.stub.CallStreamObserver;
import lombok.Builder;
import lombok.extern.log4j.Log4j2;
......@@ -55,7 +57,7 @@ public class GrpcBinaryFileServerDownloader<T> {
private final AtomicBoolean started = new AtomicBoolean(false);
private final AtomicBoolean downloadFinished = new AtomicBoolean(false);
private final AtomicBoolean requestFinished = new AtomicBoolean(false);
private final AtomicReference<BinaryFileDownloadException> error = new AtomicReference<>();
private final AtomicReference<TechnicalException> error = new AtomicReference<>();
private PipedInputStream inputStream;
private PipedOutputStream outputStream;
......@@ -92,7 +94,7 @@ public class GrpcBinaryFileServerDownloader<T> {
} catch (Exception e) {
closeOutputStream();
closeInputStream();
throw new BinaryFileDownloadException(e);
throw new TechnicalException("Error while setting up streams", e);
}
}
......@@ -106,7 +108,7 @@ public class GrpcBinaryFileServerDownloader<T> {
try {
doDownload();
} catch (Exception e) {
error.set(new BinaryFileDownloadException(e));
error.set(new TechnicalException("Error while downloading file contents", e));
} finally {
closeOutputStream();
sendChunks();
......@@ -124,7 +126,7 @@ public class GrpcBinaryFileServerDownloader<T> {
try {
doSendChunks();
} catch (Exception e) {
completeRequestWithError(e);
completeRequestWithError(new TechnicalException("Error while sending chunks", e));
}
}
......@@ -134,7 +136,7 @@ public class GrpcBinaryFileServerDownloader<T> {
}
int bytesRead;
while (isReady()) {
if (error.get() != null) {
if (Objects.nonNull(error.get())) {
completeRequestWithError(error.get());
break;
}
......@@ -167,11 +169,11 @@ public class GrpcBinaryFileServerDownloader<T> {
callObserver.onCompleted();
}
void completeRequestWithError(Throwable t) {
LOG.debug("Complete download request with error", t);
void completeRequestWithError(TechnicalException e) {
LOG.debug("Complete download request with error", e);
requestFinished.set(true);
closeInputStream();
callObserver.onError(t);
callObserver.onError(e);
}
void closeOutputStream() {
......
......@@ -191,7 +191,7 @@ class GrpcBinaryFileServerDownloaderTest {
@Test
void shouldThrowBinaryFileDownloadException() {
assertThatThrownBy(() -> downloader.safelySetupStreams()).isInstanceOf(BinaryFileDownloadException.class).hasCause(exception);
assertThatThrownBy(() -> downloader.safelySetupStreams()).isInstanceOf(TechnicalException.class).hasCause(exception);
}
@SneakyThrows
......@@ -278,7 +278,7 @@ class GrpcBinaryFileServerDownloaderTest {
void shouldSetError() {
downloader.startDownload();
assertThat(getError()).isInstanceOf(BinaryFileDownloadException.class).hasCause(exception);
assertThat(getError()).isInstanceOf(TechnicalException.class).hasCause(exception);
}
@SneakyThrows
......@@ -354,7 +354,9 @@ class GrpcBinaryFileServerDownloaderTest {
@Nested
class OnException {
private final TechnicalException exception = new TechnicalException("error");
private final IOException exception = new IOException();
@Captor
private ArgumentCaptor<TechnicalException> argumentCaptor;
@SneakyThrows
@BeforeEach
......@@ -366,7 +368,8 @@ class GrpcBinaryFileServerDownloaderTest {
void shouldCompleteRequestWithError() {
downloader.sendChunks();
verify(downloader).completeRequestWithError(exception);
verify(downloader).completeRequestWithError(argumentCaptor.capture());
assertThat(argumentCaptor.getValue()).isInstanceOf(TechnicalException.class).hasCause(exception);
}
}
}
......@@ -421,7 +424,7 @@ class GrpcBinaryFileServerDownloaderTest {
@Nested
class OnHasError {
private final BinaryFileDownloadException exception = new BinaryFileDownloadException(new TechnicalException("error"));
private final TechnicalException exception = new TechnicalException("error");
@BeforeEach
void init() {
......@@ -624,7 +627,7 @@ class GrpcBinaryFileServerDownloaderTest {
@Nested
class TestCompleteRequestWithError {
private final Throwable error = new Throwable();
private final TechnicalException error = new TechnicalException("error");
@BeforeEach
void init() {
......@@ -671,12 +674,12 @@ class GrpcBinaryFileServerDownloaderTest {
return ReflectionTestUtils.getField(downloader, "requestFinished", AtomicBoolean.class).get();
}
private void setErrorField(BinaryFileDownloadException error) {
private void setErrorField(TechnicalException error) {
ReflectionTestUtils.setField(downloader, "error", new AtomicReference<>(error));
}
private BinaryFileDownloadException getError() {
return (BinaryFileDownloadException) ReflectionTestUtils.getField(downloader, "error", AtomicReference.class).get();
private TechnicalException getError() {
return (TechnicalException) ReflectionTestUtils.getField(downloader, "error", AtomicReference.class).get();
}
private void setDownloadFinishedField(boolean downloadFinished) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment