From 8e600dca3db53677e92199a8cf80c68b1e9927dd Mon Sep 17 00:00:00 2001 From: q_h Date: Fri, 24 Jul 2020 12:19:04 +0300 Subject: [PATCH] refactor: errorHandler --- src/uploadx/lib/error-handler.spec.ts | 12 ++++---- src/uploadx/lib/error-handler.ts | 40 ++++++++++++++------------- src/uploadx/lib/uploader.ts | 6 ++-- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/uploadx/lib/error-handler.spec.ts b/src/uploadx/lib/error-handler.spec.ts index b862bbe7..78d53fca 100644 --- a/src/uploadx/lib/error-handler.spec.ts +++ b/src/uploadx/lib/error-handler.spec.ts @@ -1,12 +1,10 @@ import { ErrorHandler } from './error-handler'; describe('ErrorHandler', () => { - describe('constructor', () => { - it('should set to defaults if no parameters are specified', () => { - const errorHandler = new ErrorHandler(); - expect(errorHandler.attempts).toBe(1); - expect(errorHandler.min).toBe(500); - expect(errorHandler.max).toBe(60_000); - }); + it('should set to defaults if no parameters are specified', () => { + const errorHandler = new ErrorHandler(); + expect(errorHandler.attempts).toBe(0); + expect(ErrorHandler.min).toBe(500); + expect(ErrorHandler.max).toBe(60_000); }); }); diff --git a/src/uploadx/lib/error-handler.ts b/src/uploadx/lib/error-handler.ts index 767687d0..5efe8017 100644 --- a/src/uploadx/lib/error-handler.ts +++ b/src/uploadx/lib/error-handler.ts @@ -2,33 +2,32 @@ * @internal */ export enum ErrorType { - Restart, + NotFound, Auth, Retryable, - FatalError + Fatal } +/** + * @internal + */ export class ErrorHandler { static maxAttempts = 8; static shouldRestartCodes = [404, 410]; static authErrorCodes = [401]; static shouldRetryCodes = [423, 429]; - min = 500; - max = this.min * 120; - factor = 2; - attempts = 1; - private delay: number; + static min = 500; + static max = ErrorHandler.min * 120; + static factor = 2; + public attempts = 0; + private delay: number = ErrorHandler.min; private code? = -1; - constructor() { - this.delay = this.min; - } - kind(code: number): ErrorType { if (code === this.code) { this.attempts++; - if (this.attempts > ErrorHandler.maxAttempts) { - return ErrorType.FatalError; + if (this.attempts >= ErrorHandler.maxAttempts) { + return ErrorType.Fatal; } } else { this.reset(); @@ -39,24 +38,27 @@ export class ErrorHandler { return ErrorType.Auth; } if (ErrorHandler.shouldRestartCodes.includes(code)) { - return ErrorType.Restart; + return ErrorType.NotFound; } if (code < 400 || code >= 500 || ErrorHandler.shouldRetryCodes.includes(code)) { return ErrorType.Retryable; } - return ErrorType.FatalError; + return ErrorType.Fatal; } wait(): Promise { return new Promise(resolve => { - this.delay = Math.min(this.delay * this.factor, this.max); - setTimeout(() => resolve(this.attempts), this.delay + Math.floor(Math.random() * this.min)); + this.delay = Math.min(this.delay * ErrorHandler.factor, ErrorHandler.max); + setTimeout( + () => resolve(this.attempts), + this.delay + Math.floor(Math.random() * ErrorHandler.min) + ); }); } reset(): void { - this.delay = this.min; - this.attempts = 1; + this.delay = ErrorHandler.min; + this.attempts = 0; this.code = -1; } } diff --git a/src/uploadx/lib/uploader.ts b/src/uploadx/lib/uploader.ts index f302a1ad..6a31adac 100644 --- a/src/uploadx/lib/uploader.ts +++ b/src/uploadx/lib/uploader.ts @@ -131,7 +131,7 @@ export abstract class Uploader implements UploadState { this.start().then(_ => {}); } catch (e) { e instanceof Error && console.error(e); - if (this.errorHandler.kind(this.responseStatus) !== ErrorType.FatalError) { + if (this.errorHandler.kind(this.responseStatus) !== ErrorType.Fatal) { this.status = 'retry'; await this.errorHandler.wait(); this.status = 'queue'; @@ -162,9 +162,9 @@ export abstract class Uploader implements UploadState { const errType = this.errorHandler.kind(this.responseStatus); if (this.responseStatus === 413) { DynamicChunk.maxSize = this.chunkSize /= 2; - } else if (errType === ErrorType.FatalError) { + } else if (errType === ErrorType.Fatal) { this.status = 'error'; - } else if (errType === ErrorType.Restart) { + } else if (errType === ErrorType.NotFound) { this.url = ''; this.status = 'queue'; } else if (errType === ErrorType.Auth) {