From 359a5d17ec6028ad81331f1b50bc92155ad72698 Mon Sep 17 00:00:00 2001 From: DJ Madeira Date: Wed, 17 May 2017 13:21:53 -0400 Subject: [PATCH] Use class syntax for errors (#306) --- index.js | 106 +++++++++++++++++++++++++++++++-------------------- package.json | 1 - 2 files changed, 65 insertions(+), 42 deletions(-) diff --git a/index.js b/index.js index 160b0ff..07d55dc 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,6 @@ const urlParseLax = require('url-parse-lax'); const lowercaseKeys = require('lowercase-keys'); const isRedirect = require('is-redirect'); const decompressResponse = require('decompress-response'); -const createErrorClass = require('create-error-class'); const isRetryAllowed = require('is-retry-allowed'); const Buffer = require('safe-buffer').Buffer; const isURL = require('isurl'); @@ -371,50 +370,75 @@ for (const el of helpers) { got.stream[el] = (url, opts) => got.stream(url, Object.assign({}, opts, {method: el})); } -function stdError(error, opts) { - if (error.code !== undefined) { - this.code = error.code; - } +class StdError extends Error { + constructor(message, error, opts) { + super(message); + this.name = 'StdError'; - Object.assign(this, { - message: error.message, - host: opts.host, - hostname: opts.hostname, - method: opts.method, - path: opts.path, - protocol: opts.protocol, - url: opts.href - }); -} + if (error.code !== undefined) { + this.code = error.code; + } -got.RequestError = createErrorClass('RequestError', stdError); -got.ReadError = createErrorClass('ReadError', stdError); -got.ParseError = createErrorClass('ParseError', function (e, statusCode, opts, data) { - stdError.call(this, e, opts); - this.statusCode = statusCode; - this.statusMessage = http.STATUS_CODES[this.statusCode]; - this.message = `${e.message} in "${urlLib.format(opts)}": \n${data.slice(0, 77)}...`; -}); + Object.assign(this, { + host: opts.host, + hostname: opts.hostname, + method: opts.method, + path: opts.path, + protocol: opts.protocol, + url: opts.href + }); + } +} -got.HTTPError = createErrorClass('HTTPError', function (statusCode, headers, opts) { - stdError.call(this, {}, opts); - this.statusCode = statusCode; - this.statusMessage = http.STATUS_CODES[this.statusCode]; - this.message = `Response code ${this.statusCode} (${this.statusMessage})`; - this.headers = headers; -}); +got.RequestError = class extends StdError { + constructor(error, opts) { + super(error.message, error, opts); + this.name = 'RequestError'; + } +}; -got.MaxRedirectsError = createErrorClass('MaxRedirectsError', function (statusCode, redirectUrls, opts) { - stdError.call(this, {}, opts); - this.statusCode = statusCode; - this.statusMessage = http.STATUS_CODES[this.statusCode]; - this.message = 'Redirected 10 times. Aborting.'; - this.redirectUrls = redirectUrls; -}); +got.ReadError = class extends StdError { + constructor(error, opts) { + super(error.message, error, opts); + this.name = 'ReadError'; + } +}; + +got.ParseError = class extends StdError { + constructor(error, statusCode, opts, data) { + super(`${error.message} in "${urlLib.format(opts)}": \n${data.slice(0, 77)}...`, error, opts); + this.name = 'ParseError'; + this.statusCode = statusCode; + this.statusMessage = http.STATUS_CODES[this.statusCode]; + } +}; + +got.HTTPError = class extends StdError { + constructor(statusCode, headers, opts) { + const statusMessage = http.STATUS_CODES[statusCode]; + super(`Response code ${statusCode} (${statusMessage})`, {}, opts); + this.name = 'HTTPError'; + this.statusCode = statusCode; + this.statusMessage = statusMessage; + this.headers = headers; + } +}; + +got.MaxRedirectsError = class extends StdError { + constructor(statusCode, redirectUrls, opts) { + super('Redirected 10 times. Aborting.', {}, opts); + this.name = 'MaxRedirectsError'; + this.statusCode = statusCode; + this.statusMessage = http.STATUS_CODES[this.statusCode]; + this.redirectUrls = redirectUrls; + } +}; -got.UnsupportedProtocolError = createErrorClass('UnsupportedProtocolError', function (opts) { - stdError.call(this, {}, opts); - this.message = `Unsupported protocol "${opts.protocol}"`; -}); +got.UnsupportedProtocolError = class extends StdError { + constructor(opts) { + super(`Unsupported protocol "${opts.protocol}"`, {}, opts); + this.name = 'UnsupportedProtocolError'; + } +}; module.exports = got; diff --git a/package.json b/package.json index 48beeb6..34f0b99 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,6 @@ "fetch" ], "dependencies": { - "create-error-class": "^3.0.0", "decompress-response": "^3.2.0", "duplexer3": "^0.1.4", "get-stream": "^3.0.0",