Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove code duplication in custom errors #842

Merged
merged 2 commits into from Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/body.js
Expand Up @@ -9,8 +9,9 @@ import Stream, {PassThrough} from 'stream';
import {types} from 'util';

import Blob from 'fetch-blob';
import FetchError from './errors/fetch-error.js';
import {isBlob, isURLSearchParameters, isAbortError} from './utils/is.js';
import {FetchError} from './errors/fetch-error.js';
import {FetchBaseError} from './errors/base.js';
import {isBlob, isURLSearchParameters} from './utils/is.js';

const INTERNALS = Symbol('Body internals');

Expand Down Expand Up @@ -60,7 +61,7 @@ export default class Body {

if (body instanceof Stream) {
body.on('error', err => {
const error = isAbortError(err) ?
const error = err instanceof FetchBaseError ?
err :
new FetchError(`Invalid response body while trying to fetch ${this.url}: ${err.message}`, 'system', err);
this[INTERNALS].error = error;
Expand Down Expand Up @@ -198,7 +199,7 @@ async function consumeBody(data) {
accum.push(chunk);
}
} catch (error) {
if (isAbortError(error) || error instanceof FetchError) {
if (error instanceof FetchBaseError) {
throw error;
} else {
// Other errors, such as incorrect content-encoding
Expand Down
27 changes: 5 additions & 22 deletions src/errors/abort-error.js
@@ -1,27 +1,10 @@
/**
* Abort-error.js
*
* AbortError interface for cancelled requests
*/
import {FetchBaseError} from './base.js';

/**
* Create AbortError instance
*
* @param String message Error message for human
* @param String type Error type for machine
* @param String systemError For Node.js system error
* @return AbortError
* AbortError interface for cancelled requests
*/
export default class AbortError extends Error {
constructor(message) {
super(message);

this.type = 'aborted';
this.message = message;
this.name = 'AbortError';
this[Symbol.toStringTag] = 'AbortError';

// Hide custom error implementation details from end-users
Error.captureStackTrace(this, this.constructor);
export class AbortError extends FetchBaseError {
constructor(message, type = 'aborted') {
super(message, type);
}
}
20 changes: 20 additions & 0 deletions src/errors/base.js
@@ -0,0 +1,20 @@
'use strict';

export class FetchBaseError extends Error {
constructor(message, type) {
super(message);
// Hide custom error implementation details from end-users
Error.captureStackTrace(this, this.constructor);

this.type = type;
}

get name() {
return this.constructor.name;
}

get [Symbol.toStringTag]() {
return this.constructor.name;
}
}

36 changes: 14 additions & 22 deletions src/errors/fetch-error.js
@@ -1,34 +1,26 @@

import {FetchBaseError} from './base.js';

/**
* Fetch-error.js
*
* FetchError interface for operational errors
*/
* @typedef {{ address?: string, code: string, dest?: string, errno: number, info?: object, message: string, path?: string, port?: number, syscall: string}} SystemError
*/

/**
* Create FetchError instance
*
* @param String message Error message for human
* @param String type Error type for machine
* @param Object systemError For Node.js system error
* @return FetchError
* FetchError interface for operational errors
*/
export default class FetchError extends Error {
export class FetchError extends FetchBaseError {
/**
* @param {string} message - Error message for human
* @param {string} [type] - Error type for machine
* @param {SystemError} [systemError] - For Node.js system error
*/
constructor(message, type, systemError) {
super(message);

this.message = message;
this.type = type;
this.name = 'FetchError';
this[Symbol.toStringTag] = 'FetchError';

super(message, type);
// When err.type is `system`, err.erroredSysCall contains system error and err.code contains system error code
if (systemError) {
// eslint-disable-next-line no-multi-assign
this.code = this.errno = systemError.code;
this.erroredSysCall = systemError;
this.erroredSysCall = systemError.syscall;
}

// Hide custom error implementation details from end-users
Error.captureStackTrace(this, this.constructor);
}
}
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -16,8 +16,8 @@ import {writeToStream, getTotalBytes} from './body.js';
import Response from './response.js';
import Headers, {fromRawHeaders} from './headers.js';
import Request, {getNodeRequestOptions} from './request.js';
import FetchError from './errors/fetch-error.js';
import AbortError from './errors/abort-error.js';
import {FetchError} from './errors/fetch-error.js';
import {AbortError} from './errors/abort-error.js';
import {isRedirect} from './utils/is-redirect.js';

export {Headers, Request, Response, FetchError, AbortError, isRedirect};
Expand Down
9 changes: 0 additions & 9 deletions src/utils/is.js
Expand Up @@ -57,12 +57,3 @@ export const isAbortSignal = object => {
);
};

/**
* Check if `obj` is an instance of AbortError.
*
* @param {*} obj
* @return {boolean}
*/
export const isAbortError = object => {
return object[NAME] === 'AbortError';
};
2 changes: 1 addition & 1 deletion test/main.js
Expand Up @@ -28,7 +28,7 @@ import fetch, {
Request,
Response
} from '../src/index.js';
import FetchErrorOrig from '../src/errors/fetch-error.js';
import {FetchError as FetchErrorOrig} from '../src/errors/fetch-error.js';
import HeadersOrig, {fromRawHeaders} from '../src/headers.js';
import RequestOrig from '../src/request.js';
import ResponseOrig from '../src/response.js';
Expand Down