Browse Source

Use class syntax for errors (#306)

remove-strictssl-option
DJ Madeira 8 years ago
committed by Sindre Sorhus
parent
commit
359a5d17ec
  1. 106
      index.js
  2. 1
      package.json

106
index.js

@ -13,7 +13,6 @@ const urlParseLax = require('url-parse-lax');
const lowercaseKeys = require('lowercase-keys'); const lowercaseKeys = require('lowercase-keys');
const isRedirect = require('is-redirect'); const isRedirect = require('is-redirect');
const decompressResponse = require('decompress-response'); const decompressResponse = require('decompress-response');
const createErrorClass = require('create-error-class');
const isRetryAllowed = require('is-retry-allowed'); const isRetryAllowed = require('is-retry-allowed');
const Buffer = require('safe-buffer').Buffer; const Buffer = require('safe-buffer').Buffer;
const isURL = require('isurl'); 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})); got.stream[el] = (url, opts) => got.stream(url, Object.assign({}, opts, {method: el}));
} }
function stdError(error, opts) { class StdError extends Error {
if (error.code !== undefined) { constructor(message, error, opts) {
this.code = error.code; super(message);
} this.name = 'StdError';
Object.assign(this, { if (error.code !== undefined) {
message: error.message, this.code = error.code;
host: opts.host, }
hostname: opts.hostname,
method: opts.method,
path: opts.path,
protocol: opts.protocol,
url: opts.href
});
}
got.RequestError = createErrorClass('RequestError', stdError); Object.assign(this, {
got.ReadError = createErrorClass('ReadError', stdError); host: opts.host,
got.ParseError = createErrorClass('ParseError', function (e, statusCode, opts, data) { hostname: opts.hostname,
stdError.call(this, e, opts); method: opts.method,
this.statusCode = statusCode; path: opts.path,
this.statusMessage = http.STATUS_CODES[this.statusCode]; protocol: opts.protocol,
this.message = `${e.message} in "${urlLib.format(opts)}": \n${data.slice(0, 77)}...`; url: opts.href
}); });
}
}
got.HTTPError = createErrorClass('HTTPError', function (statusCode, headers, opts) { got.RequestError = class extends StdError {
stdError.call(this, {}, opts); constructor(error, opts) {
this.statusCode = statusCode; super(error.message, error, opts);
this.statusMessage = http.STATUS_CODES[this.statusCode]; this.name = 'RequestError';
this.message = `Response code ${this.statusCode} (${this.statusMessage})`; }
this.headers = headers; };
});
got.MaxRedirectsError = createErrorClass('MaxRedirectsError', function (statusCode, redirectUrls, opts) { got.ReadError = class extends StdError {
stdError.call(this, {}, opts); constructor(error, opts) {
this.statusCode = statusCode; super(error.message, error, opts);
this.statusMessage = http.STATUS_CODES[this.statusCode]; this.name = 'ReadError';
this.message = 'Redirected 10 times. Aborting.'; }
this.redirectUrls = redirectUrls; };
});
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) { got.UnsupportedProtocolError = class extends StdError {
stdError.call(this, {}, opts); constructor(opts) {
this.message = `Unsupported protocol "${opts.protocol}"`; super(`Unsupported protocol "${opts.protocol}"`, {}, opts);
}); this.name = 'UnsupportedProtocolError';
}
};
module.exports = got; module.exports = got;

1
package.json

@ -47,7 +47,6 @@
"fetch" "fetch"
], ],
"dependencies": { "dependencies": {
"create-error-class": "^3.0.0",
"decompress-response": "^3.2.0", "decompress-response": "^3.2.0",
"duplexer3": "^0.1.4", "duplexer3": "^0.1.4",
"get-stream": "^3.0.0", "get-stream": "^3.0.0",

Loading…
Cancel
Save