2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-20 20:00:40 +03:00

Adding request to error objects when it is available

This commit is contained in:
Rubén Norte
2017-04-08 21:44:15 +02:00
parent e0d59eb29b
commit 22ce6db383
5 changed files with 16 additions and 10 deletions
+5 -4
View File
@@ -172,13 +172,14 @@ module.exports = function httpAdapter(config) {
// make sure the content length is not over the maxContentLength if specified // make sure the content length is not over the maxContentLength if specified
if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) { if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) {
reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded', config)); reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
config, null, lastRequest));
} }
}); });
stream.on('error', function handleStreamError(err) { stream.on('error', function handleStreamError(err) {
if (aborted) return; if (aborted) return;
reject(enhanceError(err, config)); reject(enhanceError(err, config, null, lastRequest));
}); });
stream.on('end', function handleStreamEnd() { stream.on('end', function handleStreamEnd() {
@@ -196,14 +197,14 @@ module.exports = function httpAdapter(config) {
// Handle errors // Handle errors
req.on('error', function handleRequestError(err) { req.on('error', function handleRequestError(err) {
if (aborted) return; if (aborted) return;
reject(enhanceError(err, config)); reject(enhanceError(err, config, null, req));
}); });
// Handle request timeout // Handle request timeout
if (config.timeout && !timer) { if (config.timeout && !timer) {
timer = setTimeout(function handleRequestTimeout() { timer = setTimeout(function handleRequestTimeout() {
req.abort(); req.abort();
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED')); reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req));
aborted = true; aborted = true;
}, config.timeout); }, config.timeout);
} }
+3 -2
View File
@@ -84,7 +84,7 @@ module.exports = function xhrAdapter(config) {
request.onerror = function handleError() { request.onerror = function handleError() {
// Real errors are hidden from us by the browser // Real errors are hidden from us by the browser
// onerror should only fire if it's a network error // onerror should only fire if it's a network error
reject(createError('Network Error', config)); reject(createError('Network Error', config, null, request));
// Clean up request // Clean up request
request = null; request = null;
@@ -92,7 +92,8 @@ module.exports = function xhrAdapter(config) {
// Handle timeout // Handle timeout
request.ontimeout = function handleTimeout() { request.ontimeout = function handleTimeout() {
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED')); reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED',
request));
// Clean up request // Clean up request
request = null; request = null;
+4 -3
View File
@@ -3,15 +3,16 @@
var enhanceError = require('./enhanceError'); var enhanceError = require('./enhanceError');
/** /**
* Create an Error with the specified message, config, error code, and response. * Create an Error with the specified message, config, error code, request and response.
* *
* @param {string} message The error message. * @param {string} message The error message.
* @param {Object} config The config. * @param {Object} config The config.
* @param {string} [code] The error code (for example, 'ECONNABORTED'). * @param {string} [code] The error code (for example, 'ECONNABORTED').
@ @param {Object} [request] The request.
@ @param {Object} [response] The response. @ @param {Object} [response] The response.
* @returns {Error} The created error. * @returns {Error} The created error.
*/ */
module.exports = function createError(message, config, code, response) { module.exports = function createError(message, config, code, request, response) {
var error = new Error(message); var error = new Error(message);
return enhanceError(error, config, code, response); return enhanceError(error, config, code, request, response);
}; };
+3 -1
View File
@@ -6,14 +6,16 @@
* @param {Error} error The error to update. * @param {Error} error The error to update.
* @param {Object} config The config. * @param {Object} config The config.
* @param {string} [code] The error code (for example, 'ECONNABORTED'). * @param {string} [code] The error code (for example, 'ECONNABORTED').
@ @param {Object} [request] The request.
@ @param {Object} [response] The response. @ @param {Object} [response] The response.
* @returns {Error} The error. * @returns {Error} The error.
*/ */
module.exports = function enhanceError(error, config, code, response) { module.exports = function enhanceError(error, config, code, request, response) {
error.config = config; error.config = config;
if (code) { if (code) {
error.code = code; error.code = code;
} }
error.request = request;
error.response = response; error.response = response;
return error; return error;
}; };
+1
View File
@@ -19,6 +19,7 @@ module.exports = function settle(resolve, reject, response) {
'Request failed with status code ' + response.status, 'Request failed with status code ' + response.status,
response.config, response.config,
null, null,
response.request,
response response
)); ));
} }