mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Merge pull request #345 from nickuraltsev/errors2
Improve error handling
This commit is contained in:
@@ -56,8 +56,8 @@ axios.get('/user?ID=12345')
|
|||||||
.then(function (response) {
|
.then(function (response) {
|
||||||
console.log(response);
|
console.log(response);
|
||||||
})
|
})
|
||||||
.catch(function (response) {
|
.catch(function (error) {
|
||||||
console.log(response);
|
console.log(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Optionally the request above could also be done as
|
// Optionally the request above could also be done as
|
||||||
@@ -69,8 +69,8 @@ axios.get('/user', {
|
|||||||
.then(function (response) {
|
.then(function (response) {
|
||||||
console.log(response);
|
console.log(response);
|
||||||
})
|
})
|
||||||
.catch(function (response) {
|
.catch(function (error) {
|
||||||
console.log(response);
|
console.log(error);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -84,8 +84,8 @@ axios.post('/user', {
|
|||||||
.then(function (response) {
|
.then(function (response) {
|
||||||
console.log(response);
|
console.log(response);
|
||||||
})
|
})
|
||||||
.catch(function (response) {
|
.catch(function (error) {
|
||||||
console.log(response);
|
console.log(error);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -414,18 +414,18 @@ instance.interceptors.request.use(function () {/*...*/});
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
axios.get('/user/12345')
|
axios.get('/user/12345')
|
||||||
.catch(function (response) {
|
.catch(function (error) {
|
||||||
if (response instanceof Error) {
|
if (error.response) {
|
||||||
// Something happened in setting up the request that triggered an Error
|
|
||||||
console.log('Error', response.message);
|
|
||||||
} else {
|
|
||||||
// The request was made, but the server responded with a status code
|
// The request was made, but the server responded with a status code
|
||||||
// that falls out of the range of 2xx
|
// that falls out of the range of 2xx
|
||||||
console.log(response.data);
|
console.log(error.response.data);
|
||||||
console.log(response.status);
|
console.log(error.response.status);
|
||||||
console.log(response.headers);
|
console.log(error.response.headers);
|
||||||
console.log(response.config);
|
} else {
|
||||||
|
// Something happened in setting up the request that triggered an Error
|
||||||
|
console.log('Error', error.message);
|
||||||
}
|
}
|
||||||
|
console.log(error.config);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -25,8 +25,8 @@
|
|||||||
);
|
);
|
||||||
}).join('');
|
}).join('');
|
||||||
})
|
})
|
||||||
.catch(function(data) {
|
.catch(function (err) {
|
||||||
document.getElementById('people').innerHTML = '<li class="text-danger">' + data + '</li>';
|
document.getElementById('people').innerHTML = '<li class="text-danger">' + err.message + '</li>';
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@@ -29,9 +29,9 @@
|
|||||||
output.className = 'container';
|
output.className = 'container';
|
||||||
output.innerHTML = res.data;
|
output.innerHTML = res.data;
|
||||||
})
|
})
|
||||||
.catch(function (res) {
|
.catch(function (err) {
|
||||||
output.className = 'container text-danger';
|
output.className = 'container text-danger';
|
||||||
output.innerHTML = res.data;
|
output.innerHTML = err.message;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|||||||
@@ -37,9 +37,9 @@
|
|||||||
output.className = 'container';
|
output.className = 'container';
|
||||||
output.innerHTML = res.data;
|
output.innerHTML = res.data;
|
||||||
})
|
})
|
||||||
.catch(function (res) {
|
.catch(function (err) {
|
||||||
output.className = 'container text-danger';
|
output.className = 'container text-danger';
|
||||||
output.innerHTML = res.data;
|
output.innerHTML = err.message;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|||||||
+10
-8
@@ -12,6 +12,8 @@ var url = require('url');
|
|||||||
var zlib = require('zlib');
|
var zlib = require('zlib');
|
||||||
var pkg = require('./../../package.json');
|
var pkg = require('./../../package.json');
|
||||||
var Buffer = require('buffer').Buffer;
|
var Buffer = require('buffer').Buffer;
|
||||||
|
var createError = require('../core/createError');
|
||||||
|
var enhanceError = require('../core/enhanceError');
|
||||||
|
|
||||||
/*eslint consistent-return:0*/
|
/*eslint consistent-return:0*/
|
||||||
module.exports = function httpAdapter(resolve, reject, config) {
|
module.exports = function httpAdapter(resolve, reject, config) {
|
||||||
@@ -33,7 +35,10 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
|||||||
} else if (utils.isString(data)) {
|
} else if (utils.isString(data)) {
|
||||||
data = new Buffer(data, 'utf-8');
|
data = new Buffer(data, 'utf-8');
|
||||||
} else {
|
} else {
|
||||||
return reject(new Error('Data after transformation must be a string, an ArrayBuffer, or a Stream'));
|
return reject(createError(
|
||||||
|
'Data after transformation must be a string, an ArrayBuffer, or a Stream',
|
||||||
|
config
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add Content-Length header if data exists
|
// Add Content-Length header if data exists
|
||||||
@@ -122,13 +127,13 @@ module.exports = function httpAdapter(resolve, reject, 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(new Error('maxContentLength size of ' + config.maxContentLength + ' exceeded'));
|
reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded', config));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
stream.on('error', function handleStreamError(err) {
|
stream.on('error', function handleStreamError(err) {
|
||||||
if (aborted) return;
|
if (aborted) return;
|
||||||
reject(err);
|
reject(enhanceError(err, config));
|
||||||
});
|
});
|
||||||
|
|
||||||
stream.on('end', function handleStreamEnd() {
|
stream.on('end', function handleStreamEnd() {
|
||||||
@@ -145,17 +150,14 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
|||||||
// Handle errors
|
// Handle errors
|
||||||
req.on('error', function handleRequestError(err) {
|
req.on('error', function handleRequestError(err) {
|
||||||
if (aborted) return;
|
if (aborted) return;
|
||||||
reject(err);
|
reject(enhanceError(err, config));
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle request timeout
|
// Handle request timeout
|
||||||
if (config.timeout && !timer) {
|
if (config.timeout && !timer) {
|
||||||
timer = setTimeout(function handleRequestTimeout() {
|
timer = setTimeout(function handleRequestTimeout() {
|
||||||
var err = new Error('timeout of ' + config.timeout + 'ms exceeded');
|
|
||||||
err.timeout = config.timeout;
|
|
||||||
err.code = 'ECONNABORTED';
|
|
||||||
req.abort();
|
req.abort();
|
||||||
reject(err);
|
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED'));
|
||||||
aborted = true;
|
aborted = true;
|
||||||
}, config.timeout);
|
}, config.timeout);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-5
@@ -6,6 +6,7 @@ var transformData = require('./../core/transformData');
|
|||||||
var buildURL = require('./../helpers/buildURL');
|
var buildURL = require('./../helpers/buildURL');
|
||||||
var parseHeaders = require('./../helpers/parseHeaders');
|
var parseHeaders = require('./../helpers/parseHeaders');
|
||||||
var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
|
var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
|
||||||
|
var createError = require('../core/createError');
|
||||||
var btoa = (typeof window !== 'undefined' && window.btoa) || require('./../helpers/btoa');
|
var btoa = (typeof window !== 'undefined' && window.btoa) || require('./../helpers/btoa');
|
||||||
|
|
||||||
module.exports = function xhrAdapter(resolve, reject, config) {
|
module.exports = function xhrAdapter(resolve, reject, config) {
|
||||||
@@ -82,7 +83,7 @@ module.exports = function xhrAdapter(resolve, reject, 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(new Error('Network Error'));
|
reject(createError('Network Error', config));
|
||||||
|
|
||||||
// Clean up request
|
// Clean up request
|
||||||
request = null;
|
request = null;
|
||||||
@@ -90,10 +91,7 @@ module.exports = function xhrAdapter(resolve, reject, config) {
|
|||||||
|
|
||||||
// Handle timeout
|
// Handle timeout
|
||||||
request.ontimeout = function handleTimeout() {
|
request.ontimeout = function handleTimeout() {
|
||||||
var err = new Error('timeout of ' + config.timeout + 'ms exceeded');
|
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED'));
|
||||||
err.timeout = config.timeout;
|
|
||||||
err.code = 'ECONNABORTED';
|
|
||||||
reject(err);
|
|
||||||
|
|
||||||
// Clean up request
|
// Clean up request
|
||||||
request = null;
|
request = null;
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var enhanceError = require('./enhanceError');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an Error with the specified message, config, error code, and response.
|
||||||
|
*
|
||||||
|
* @param {string} message The error message.
|
||||||
|
* @param {Object} config The config.
|
||||||
|
* @param {string} [code] The error code (for example, 'ECONNABORTED').
|
||||||
|
@ @param {Object} [response] The response.
|
||||||
|
* @returns {Error} The created error.
|
||||||
|
*/
|
||||||
|
module.exports = function createError(message, config, code, response) {
|
||||||
|
var error = new Error(message);
|
||||||
|
return enhanceError(error, config, code, response);
|
||||||
|
};
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update an Error with the specified config, error code, and response.
|
||||||
|
*
|
||||||
|
* @param {Error} error The error to update.
|
||||||
|
* @param {Object} config The config.
|
||||||
|
* @param {string} [code] The error code (for example, 'ECONNABORTED').
|
||||||
|
@ @param {Object} [response] The response.
|
||||||
|
* @returns {Error} The error.
|
||||||
|
*/
|
||||||
|
module.exports = function enhanceError(error, config, code, response) {
|
||||||
|
error.config = config;
|
||||||
|
if (code) {
|
||||||
|
error.code = code;
|
||||||
|
}
|
||||||
|
error.response = response;
|
||||||
|
return error;
|
||||||
|
};
|
||||||
+8
-1
@@ -1,5 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var createError = require('./createError');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve or reject a Promise based on response status.
|
* Resolve or reject a Promise based on response status.
|
||||||
*
|
*
|
||||||
@@ -13,6 +15,11 @@ module.exports = function settle(resolve, reject, response) {
|
|||||||
if (!response.status || !validateStatus || validateStatus(response.status)) {
|
if (!response.status || !validateStatus || validateStatus(response.status)) {
|
||||||
resolve(response);
|
resolve(response);
|
||||||
} else {
|
} else {
|
||||||
reject(response);
|
reject(createError(
|
||||||
|
'Request failed with status code ' + response.status,
|
||||||
|
response.config,
|
||||||
|
null,
|
||||||
|
response
|
||||||
|
));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
var createError = require('../../../lib/core/createError');
|
||||||
|
|
||||||
|
describe('core::createError', function() {
|
||||||
|
it('should create an Error with message, config, and code', function() {
|
||||||
|
var error = createError('Boom!', { foo: 'bar' }, 'ESOMETHING');
|
||||||
|
expect(error instanceof Error).toBe(true);
|
||||||
|
expect(error.message).toBe('Boom!');
|
||||||
|
expect(error.config).toEqual({ foo: 'bar' });
|
||||||
|
expect(error.code).toBe('ESOMETHING');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
var enhanceError = require('../../../lib/core/enhanceError');
|
||||||
|
|
||||||
|
describe('core::enhanceError', function() {
|
||||||
|
it('should add config and code to error', function() {
|
||||||
|
var error = new Error('Boom!');
|
||||||
|
enhanceError(error, { foo: 'bar' }, 'ESOMETHING');
|
||||||
|
expect(error.config).toEqual({ foo: 'bar' });
|
||||||
|
expect(error.code).toBe('ESOMETHING');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return error', function() {
|
||||||
|
var error = new Error('Boom!');
|
||||||
|
expect(enhanceError(error, { foo: 'bar' }, 'ESOMETHING')).toBe(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -58,7 +58,12 @@ describe('core::settle', function() {
|
|||||||
};
|
};
|
||||||
settle(resolve, reject, response);
|
settle(resolve, reject, response);
|
||||||
expect(resolve).not.toHaveBeenCalled();
|
expect(resolve).not.toHaveBeenCalled();
|
||||||
expect(reject).toHaveBeenCalledWith(response);
|
expect(reject).toHaveBeenCalled();
|
||||||
|
var reason = reject.calls.first().args[0];
|
||||||
|
expect(reason instanceof Error).toBe(true);
|
||||||
|
expect(reason.message).toBe('Request failed with status code 500');
|
||||||
|
expect(reason.config).toBe(response.config);
|
||||||
|
expect(reason.response).toBe(response);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should pass status to validateStatus', function() {
|
it('should pass status to validateStatus', function() {
|
||||||
|
|||||||
@@ -46,6 +46,10 @@ describe('requests', function () {
|
|||||||
var finish = function () {
|
var finish = function () {
|
||||||
expect(resolveSpy).not.toHaveBeenCalled();
|
expect(resolveSpy).not.toHaveBeenCalled();
|
||||||
expect(rejectSpy).toHaveBeenCalled();
|
expect(rejectSpy).toHaveBeenCalled();
|
||||||
|
var reason = rejectSpy.calls.first().args[0];
|
||||||
|
expect(reason instanceof Error).toBe(true);
|
||||||
|
expect(reason.config.method).toBe('get');
|
||||||
|
expect(reason.config.url).toBe('http://thisisnotaserver');
|
||||||
|
|
||||||
done();
|
done();
|
||||||
};
|
};
|
||||||
@@ -68,6 +72,13 @@ describe('requests', function () {
|
|||||||
.then(function () {
|
.then(function () {
|
||||||
expect(resolveSpy).not.toHaveBeenCalled();
|
expect(resolveSpy).not.toHaveBeenCalled();
|
||||||
expect(rejectSpy).toHaveBeenCalled();
|
expect(rejectSpy).toHaveBeenCalled();
|
||||||
|
var reason = rejectSpy.calls.first().args[0];
|
||||||
|
expect(reason instanceof Error).toBe(true);
|
||||||
|
expect(reason.message).toBe('Request failed with status code 500');
|
||||||
|
expect(reason.config.method).toBe('get');
|
||||||
|
expect(reason.config.url).toBe('/foo');
|
||||||
|
expect(reason.response.status).toBe(500);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ module.exports = {
|
|||||||
timeout: 250
|
timeout: 250
|
||||||
}).then(function (res) {
|
}).then(function (res) {
|
||||||
success = true;
|
success = true;
|
||||||
}).catch(function (res) {
|
}).catch(function (err) {
|
||||||
error = res;
|
error = err;
|
||||||
failure = true;
|
failure = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -189,8 +189,8 @@ module.exports = {
|
|||||||
maxContentLength: 2000
|
maxContentLength: 2000
|
||||||
}).then(function (res) {
|
}).then(function (res) {
|
||||||
success = true;
|
success = true;
|
||||||
}).catch(function (res) {
|
}).catch(function (err) {
|
||||||
error = res;
|
error = err;
|
||||||
failure = true;
|
failure = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user