mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Improve error handling
This commit is contained in:
+10
-8
@@ -12,6 +12,8 @@ var url = require('url');
|
||||
var zlib = require('zlib');
|
||||
var pkg = require('./../../package.json');
|
||||
var Buffer = require('buffer').Buffer;
|
||||
var createError = require('../core/createError');
|
||||
var enhanceError = require('../core/enhanceError');
|
||||
|
||||
/*eslint consistent-return:0*/
|
||||
module.exports = function httpAdapter(resolve, reject, config) {
|
||||
@@ -33,7 +35,10 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
||||
} else if (utils.isString(data)) {
|
||||
data = new Buffer(data, 'utf-8');
|
||||
} 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
|
||||
@@ -122,13 +127,13 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
||||
|
||||
// make sure the content length is not over the maxContentLength if specified
|
||||
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) {
|
||||
if (aborted) return;
|
||||
reject(err);
|
||||
reject(enhanceError(err, config));
|
||||
});
|
||||
|
||||
stream.on('end', function handleStreamEnd() {
|
||||
@@ -145,17 +150,14 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
||||
// Handle errors
|
||||
req.on('error', function handleRequestError(err) {
|
||||
if (aborted) return;
|
||||
reject(err);
|
||||
reject(enhanceError(err, config));
|
||||
});
|
||||
|
||||
// Handle request timeout
|
||||
if (config.timeout && !timer) {
|
||||
timer = setTimeout(function handleRequestTimeout() {
|
||||
var err = new Error('timeout of ' + config.timeout + 'ms exceeded');
|
||||
err.timeout = config.timeout;
|
||||
err.code = 'ECONNABORTED';
|
||||
req.abort();
|
||||
reject(err);
|
||||
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED'));
|
||||
aborted = true;
|
||||
}, config.timeout);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user