From cd1464c8809cdd740a2b2a1cdf3d6793ca38e496 Mon Sep 17 00:00:00 2001
From: Martin <git@mail.de>
Date: Wed, 12 Feb 2025 22:29:01 +0100
Subject: [PATCH] OZG-5977 implement catch error for upload file

---
 .../src/lib/binary-file.service.spec.ts       | 31 +++++++++++--------
 .../src/lib/binary-file.service.ts            |  3 +-
 alfa-client/libs/tech-shared/test/marbles.ts  | 13 ++++++++
 3 files changed, 33 insertions(+), 14 deletions(-)

diff --git a/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.spec.ts b/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.spec.ts
index 3e21ff0e6d..0224be702b 100644
--- a/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.spec.ts
+++ b/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.spec.ts
@@ -21,7 +21,14 @@
  * Die sprachspezifischen Genehmigungen und Beschränkungen
  * unter der Lizenz sind dem Lizenztext zu entnehmen.
  */
-import { BlobWithFileName, createEmptyStateResource, createStateResource, StateResource } from '@alfa-client/tech-shared';
+import {
+  BlobWithFileName,
+  createEmptyStateResource,
+  createErrorStateResource,
+  createStateResource,
+  HttpError,
+  StateResource,
+} from '@alfa-client/tech-shared';
 import { Mock, mock, useFromMock } from '@alfa-client/test-utils';
 import { SnackBarService } from '@alfa-client/ui';
 import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
@@ -32,10 +39,11 @@ import { cold, hot } from 'jest-marbles';
 import { createBinaryFileResource, createBlob, createFile } from 'libs/binary-file-shared/test/binary-file';
 import { VALIDATION_MESSAGES, ValidationMessageCode } from 'libs/tech-shared/src/lib/validation/tech.validation.messages';
 import { DummyLinkRel } from 'libs/tech-shared/test/dummy';
+import { createProblemDetail } from 'libs/tech-shared/test/error';
 import { createDummyResource } from 'libs/tech-shared/test/resource';
 import { Observable, of, throwError } from 'rxjs';
 import { createHttpErrorResponse } from '../../../tech-shared/test/http';
-import { singleCold, singleHot } from '../../../tech-shared/test/marbles';
+import { coldError, coldStartWithError, multipleCold, singleCold, singleHot } from '../../../tech-shared/test/marbles';
 import { BinaryFileResource, BinaryFileUploadType, ToUploadFile, UploadFile } from './binary-file.model';
 import { BinaryFileRepository } from './binary-file.repository';
 import { BinaryFileService } from './binary-file.service';
@@ -149,15 +157,17 @@ describe('BinaryFileService', () => {
         const response: Observable<StateResource<BinaryFileResource>> = service._handleUpload(toUploadFile);
 
         expect(response).toBeObservable(
-          cold('ab', {
-            a: createEmptyStateResource(true),
-            b: createStateResource(binaryFileStateResource),
-          }),
+          multipleCold(createEmptyStateResource(true), createStateResource(binaryFileStateResource)),
         );
       });
 
       it('should return error state resource', () => {
-        //Implement me
+        const error: HttpError = createProblemDetail();
+        repository.uploadFileNew.mockReturnValue(coldError(error));
+
+        const response: Observable<StateResource<BinaryFileResource>> = service._handleUpload(toUploadFile);
+
+        expect(response).toBeObservable(coldStartWithError(createEmptyStateResource(true), createErrorStateResource(error)));
       });
     });
   });
@@ -184,12 +194,7 @@ describe('BinaryFileService', () => {
 
       const returnValue: Observable<StateResource<Blob>> = service.downloadFile(binaryFileResource, downloadNamePrefix);
 
-      expect(returnValue).toBeObservable(
-        cold('ab', {
-          a: createEmptyStateResource(true),
-          b: createStateResource(blob),
-        }),
-      );
+      expect(returnValue).toBeObservable(multipleCold(createEmptyStateResource(true), createStateResource(blob)));
     });
 
     describe('save data', () => {
diff --git a/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.ts b/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.ts
index 490f0b8571..02513d3055 100644
--- a/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.ts
+++ b/alfa-client/libs/binary-file-shared/src/lib/binary-file.service.ts
@@ -29,6 +29,7 @@ import {
   EMPTY_ARRAY,
   EMPTY_STRING,
   getMessageForInvalidParam,
+  HttpError,
   HttpHeader,
   isNotNil,
   isUnprocessableEntity,
@@ -95,7 +96,7 @@ export class BinaryFileService {
   _handleUpload(toUploadFile: ToUploadFile): Observable<StateResource<BinaryFileResource>> {
     return this.repository.uploadFileNew(toUploadFile.uri, toUploadFile.file).pipe(
       map((binaryFileResource: BinaryFileResource) => createStateResource(binaryFileResource)),
-      //Implement me => catchError
+      catchError((response: any) => of(createErrorStateResource(<HttpError>response.error))),
       startWith(createEmptyStateResource<BinaryFileResource>(true)),
     );
   }
diff --git a/alfa-client/libs/tech-shared/test/marbles.ts b/alfa-client/libs/tech-shared/test/marbles.ts
index ea8e042375..b041e6ed53 100644
--- a/alfa-client/libs/tech-shared/test/marbles.ts
+++ b/alfa-client/libs/tech-shared/test/marbles.ts
@@ -22,6 +22,7 @@
  * unter der Lizenz sind dem Lizenztext zu entnehmen.
  */
 import { ObservableWithSubscriptions, cold, hot } from 'jest-marbles';
+import { HttpError } from '../src/lib/tech.model';
 
 export function singleHot(object: any, frame: string = 'a'): ObservableWithSubscriptions {
   return hot(frame, { a: object });
@@ -34,3 +35,15 @@ export function singleCold(object: any, frame: string = 'a'): ObservableWithSubs
 export function singleColdCompleted(object: any, frame: string = 'a'): ObservableWithSubscriptions {
   return cold(`(${frame}|)`, { a: object });
 }
+
+export function multipleCold(first: any, second: any): ObservableWithSubscriptions {
+  return cold('ab', { a: first, b: second });
+}
+
+export function coldError(error: HttpError): ObservableWithSubscriptions {
+  return cold('-#', null, { error });
+}
+
+export function coldStartWithError(startWith: any, error: any): ObservableWithSubscriptions {
+  return cold('a(b|)', { a: startWith, b: error });
+}
-- 
GitLab