UNPKG

@dasch-swiss/dsp-js

Version:

JavaScript library that handles API requests to Knora

42 lines 1.73 kB
import { of, throwError } from "rxjs"; import { delay, mergeMap, retryWhen, tap } from "rxjs/operators"; /** * * Retries failed HTTP requests. * * @param delayMs delay in milliseconds before the request is retried. * @param maxRetries maximum number of retries. * @param retryOnErrorStatus HTTP error status codes for which the request is retried. * @param logError if true, error is written to the console error log. * * @category Internal */ export function retryOnError(delayMs, maxRetries, retryOnErrorStatus, logError) { var retries = maxRetries; // inspired by https://medium.com/angular-in-depth/retry-failed-http-requests-in-angular-f5959d486294 return function (src) { return src.pipe( // when no error is thrown, this simply returns the source Observable retryWhen(function (errors) { return errors.pipe( // log error message if logging is enabled tap(function (error) { if (logError) console.error("HTTP request failed:", "status:", error.status, "retries:", retries, "error:", error); }), mergeMap(function (error) { // retry on specified error status // check if max retries is reached ("retries" is decremented on each retry) if (retryOnErrorStatus.indexOf(error.status) !== -1 && retries-- > 0) { return of(error).pipe( // delay retry delay(delayMs)); } else { // do not retry return throwError(error); } })); })); }; } //# sourceMappingURL=retryOnError.js.map