From 3178ac95b7091dc0cd16f47d81d2d6488f4dea3d Mon Sep 17 00:00:00 2001 From: thomas Date: Thu, 15 Dec 2022 17:19:46 +0100 Subject: [PATCH] feat(fakeRandomErrorHttp): add a random error http call --- .../utils/src/lib/random-http-error.utils.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/libs/shared/utils/src/lib/random-http-error.utils.ts b/libs/shared/utils/src/lib/random-http-error.utils.ts index 7ed0f97..f77406a 100644 --- a/libs/shared/utils/src/lib/random-http-error.utils.ts +++ b/libs/shared/utils/src/lib/random-http-error.utils.ts @@ -1,4 +1,5 @@ import { randNumber } from '@ngneat/falso'; +import { Observable, throwError } from 'rxjs'; /** * Execute success function if value is above threshold and error function otherwise @@ -22,3 +23,25 @@ export const randomError = ({ } return error(); }; + +/** + * Execute http request if value is above threshold and throw an error otherwise + * @param httpRequest + * @param errorMessage + * @param threashold default to 0.5 + */ +export const randomErrorHttp = ({ + httpRequest, + errorMessage, + threashold, +}: { + httpRequest: () => Observable; + errorMessage?: string; + threashold?: number; +}): Observable => { + const randomNumber = randNumber({ min: 0.1, max: 1, fraction: 2 }); + if (randomNumber > (threashold ?? 0.5)) { + return httpRequest(); + } + return throwError(() => new Error(errorMessage ?? 'Http Error')); +};