mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-10 12:53:03 -05:00
feat(fakeRandomErrorHttp): add a random error http call
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { randNumber } from '@ngneat/falso';
|
import { randNumber } from '@ngneat/falso';
|
||||||
|
import { Observable, throwError } from 'rxjs';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute success function if value is above threshold and error function otherwise
|
* Execute success function if value is above threshold and error function otherwise
|
||||||
@@ -22,3 +23,25 @@ export const randomError = <T>({
|
|||||||
}
|
}
|
||||||
return error();
|
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'));
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user