feat(fakeRandomErrorHttp): add a random error http call

This commit is contained in:
thomas
2022-12-15 17:19:46 +01:00
parent c60791a530
commit 3178ac95b7

View File

@@ -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 = <T>({
}
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 = <T>({
httpRequest,
errorMessage,
threashold,
}: {
httpRequest: () => Observable<T>;
errorMessage?: string;
threashold?: number;
}): Observable<T> => {
const randomNumber = randNumber({ min: 0.1, max: 1, fraction: 2 });
if (randomNumber > (threashold ?? 0.5)) {
return httpRequest();
}
return throwError(() => new Error(errorMessage ?? 'Http Error'));
};