Skip to content

Commit

Permalink
refactor: errorHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
kukhariev committed Jul 24, 2020
1 parent e6d2cf6 commit 8e600dc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
12 changes: 5 additions & 7 deletions src/uploadx/lib/error-handler.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
40 changes: 21 additions & 19 deletions src/uploadx/lib/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<number> {
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;
}
}
6 changes: 3 additions & 3 deletions src/uploadx/lib/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8e600dc

Please sign in to comment.