Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xjbieke
axios
提交
91dae3c4
A
axios
项目概览
xjbieke
/
axios
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
axios
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
91dae3c4
编写于
6月 13, 2016
作者:
N
Nick Uraltsev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve error handling
上级
120e8f55
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
120 addition
and
41 deletion
+120
-41
README.md
README.md
+15
-15
examples/get/index.html
examples/get/index.html
+3
-3
examples/post/index.html
examples/post/index.html
+2
-2
examples/upload/index.html
examples/upload/index.html
+2
-2
lib/adapters/http.js
lib/adapters/http.js
+10
-8
lib/adapters/xhr.js
lib/adapters/xhr.js
+3
-5
lib/core/createError.js
lib/core/createError.js
+16
-0
lib/core/enhanceError.js
lib/core/enhanceError.js
+17
-0
lib/core/settle.js
lib/core/settle.js
+5
-1
test/specs/core/createError.spec.js
test/specs/core/createError.spec.js
+11
-0
test/specs/core/enhanceError.spec.js
test/specs/core/enhanceError.spec.js
+15
-0
test/specs/core/settle.spec.js
test/specs/core/settle.spec.js
+6
-1
test/specs/requests.spec.js
test/specs/requests.spec.js
+11
-0
test/unit/adapters/http.js
test/unit/adapters/http.js
+4
-4
未找到文件。
README.md
浏览文件 @
91dae3c4
...
...
@@ -56,8 +56,8 @@ axios.get('/user?ID=12345')
.
then
(
function
(
response
)
{
console
.
log
(
response
);
})
.
catch
(
function
(
response
)
{
console
.
log
(
response
);
.
catch
(
function
(
error
)
{
console
.
log
(
error
);
});
// Optionally the request above could also be done as
...
...
@@ -69,8 +69,8 @@ axios.get('/user', {
.
then
(
function
(
response
)
{
console
.
log
(
response
);
})
.
catch
(
function
(
response
)
{
console
.
log
(
response
);
.
catch
(
function
(
error
)
{
console
.
log
(
error
);
});
```
...
...
@@ -84,8 +84,8 @@ axios.post('/user', {
.
then
(
function
(
response
)
{
console
.
log
(
response
);
})
.
catch
(
function
(
response
)
{
console
.
log
(
response
);
.
catch
(
function
(
error
)
{
console
.
log
(
error
);
});
```
...
...
@@ -414,18 +414,18 @@ instance.interceptors.request.use(function () {/*...*/});
```
js
axios
.
get
(
'
/user/12345
'
)
.
catch
(
function
(
response
)
{
if
(
response
instanceof
Error
)
{
// Something happened in setting up the request that triggered an Error
console
.
log
(
'
Error
'
,
response
.
message
);
}
else
{
.
catch
(
function
(
error
)
{
if
(
error
.
response
)
{
// The request was made, but the server responded with a status code
// that falls out of the range of 2xx
console
.
log
(
response
.
data
);
console
.
log
(
response
.
status
);
console
.
log
(
response
.
headers
);
console
.
log
(
response
.
config
);
console
.
log
(
error
.
response
.
data
);
console
.
log
(
error
.
response
.
status
);
console
.
log
(
error
.
response
.
headers
);
}
else
{
// Something happened in setting up the request that triggered an Error
console
.
log
(
'
Error
'
,
error
.
message
);
}
console
.
log
(
error
.
config
);
});
```
...
...
examples/get/index.html
浏览文件 @
91dae3c4
...
...
@@ -20,13 +20,13 @@
'
<strong>
'
+
person
.
name
+
'
</strong>
'
+
'
<div>Github: <a href="https://github.com/
'
+
person
.
github
+
'
" target="_blank">
'
+
person
.
github
+
'
</a></div>
'
+
'
<div>Twitter: <a href="https://twitter.com/
'
+
person
.
twitter
+
'
" target="_blank">
'
+
person
.
twitter
+
'
</a></div>
'
+
'
</div>
'
+
'
</div>
'
+
'
</li><br/>
'
);
}).
join
(
''
);
})
.
catch
(
function
(
data
)
{
document
.
getElementById
(
'
people
'
).
innerHTML
=
'
<li class="text-danger">
'
+
data
+
'
</li>
'
;
.
catch
(
function
(
err
)
{
document
.
getElementById
(
'
people
'
).
innerHTML
=
'
<li class="text-danger">
'
+
err
.
message
+
'
</li>
'
;
});
</script>
</body>
...
...
examples/post/index.html
浏览文件 @
91dae3c4
...
...
@@ -29,9 +29,9 @@
output
.
className
=
'
container
'
;
output
.
innerHTML
=
res
.
data
;
})
.
catch
(
function
(
res
)
{
.
catch
(
function
(
err
)
{
output
.
className
=
'
container text-danger
'
;
output
.
innerHTML
=
res
.
data
;
output
.
innerHTML
=
err
.
message
;
});
};
})();
...
...
examples/upload/index.html
浏览文件 @
91dae3c4
...
...
@@ -37,9 +37,9 @@
output
.
className
=
'
container
'
;
output
.
innerHTML
=
res
.
data
;
})
.
catch
(
function
(
res
)
{
.
catch
(
function
(
err
)
{
output
.
className
=
'
container text-danger
'
;
output
.
innerHTML
=
res
.
data
;
output
.
innerHTML
=
err
.
message
;
});
};
})();
...
...
lib/adapters/http.js
浏览文件 @
91dae3c4
...
...
@@ -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
(
e
rr
);
reject
(
e
nhanceError
(
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
(
e
rr
);
reject
(
e
nhanceError
(
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
);
}
...
...
lib/adapters/xhr.js
浏览文件 @
91dae3c4
...
...
@@ -6,6 +6,7 @@ var transformData = require('./../core/transformData');
var
buildURL
=
require
(
'
./../helpers/buildURL
'
);
var
parseHeaders
=
require
(
'
./../helpers/parseHeaders
'
);
var
isURLSameOrigin
=
require
(
'
./../helpers/isURLSameOrigin
'
);
var
createError
=
require
(
'
../core/createError
'
);
var
btoa
=
(
typeof
window
!==
'
undefined
'
&&
window
.
btoa
)
||
require
(
'
./../helpers/btoa
'
);
module
.
exports
=
function
xhrAdapter
(
resolve
,
reject
,
config
)
{
...
...
@@ -82,7 +83,7 @@ module.exports = function xhrAdapter(resolve, reject, config) {
request
.
onerror
=
function
handleError
()
{
// Real errors are hidden from us by the browser
// onerror should only fire if it's a network error
reject
(
new
Error
(
'
Network Error
'
));
reject
(
createError
(
'
Network Error
'
,
config
));
// Clean up request
request
=
null
;
...
...
@@ -90,10 +91,7 @@ module.exports = function xhrAdapter(resolve, reject, config) {
// Handle timeout
request
.
ontimeout
=
function
handleTimeout
()
{
var
err
=
new
Error
(
'
timeout of
'
+
config
.
timeout
+
'
ms exceeded
'
);
err
.
timeout
=
config
.
timeout
;
err
.
code
=
'
ECONNABORTED
'
;
reject
(
err
);
reject
(
createError
(
'
timeout of
'
+
config
.
timeout
+
'
ms exceeded
'
,
config
,
'
ECONNABORTED
'
));
// Clean up request
request
=
null
;
...
...
lib/core/createError.js
0 → 100644
浏览文件 @
91dae3c4
'
use strict
'
;
var
enhanceError
=
require
(
'
./enhanceError
'
);
/**
* Create an Error with the specified message, config, and optional error code.
*
* @param {string} message The error message.
* @param {Object} config The config object.
* @param {string} [code] The error code (for example, 'ECONNABORTED').
* @returns {Error} The created error.
*/
module
.
exports
=
function
createError
(
message
,
config
,
code
)
{
var
error
=
new
Error
(
message
);
return
enhanceError
(
error
,
config
,
code
);
};
lib/core/enhanceError.js
0 → 100644
浏览文件 @
91dae3c4
'
use strict
'
;
/**
* Update an Error with the specified config and optional error code.
*
* @param {Error} error The error to update.
* @param {Object} config The config object.
* @param {string} [code] The error code (for example, 'ECONNABORTED').
* @returns {Error} The error.
*/
module
.
exports
=
function
enhanceError
(
error
,
config
,
code
)
{
error
.
config
=
config
;
if
(
code
)
{
error
.
code
=
code
;
}
return
error
;
};
lib/core/settle.js
浏览文件 @
91dae3c4
'
use strict
'
;
var
createError
=
require
(
'
./createError
'
);
/**
* Resolve or reject a Promise based on response status.
*
...
...
@@ -13,6 +15,8 @@ module.exports = function settle(resolve, reject, response) {
if
(
!
response
.
status
||
!
validateStatus
||
validateStatus
(
response
.
status
))
{
resolve
(
response
);
}
else
{
reject
(
response
);
var
error
=
createError
(
'
Request failed with status code
'
+
response
.
status
,
response
.
config
);
error
.
response
=
response
;
reject
(
error
);
}
};
test/specs/core/createError.spec.js
0 → 100644
浏览文件 @
91dae3c4
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
'
);
});
});
test/specs/core/enhanceError.spec.js
0 → 100644
浏览文件 @
91dae3c4
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
);
});
});
test/specs/core/settle.spec.js
浏览文件 @
91dae3c4
...
...
@@ -58,7 +58,12 @@ describe('core::settle', function() {
};
settle
(
resolve
,
reject
,
response
);
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
()
{
...
...
test/specs/requests.spec.js
浏览文件 @
91dae3c4
...
...
@@ -46,6 +46,10 @@ describe('requests', function () {
var
finish
=
function
()
{
expect
(
resolveSpy
).
not
.
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
();
};
...
...
@@ -68,6 +72,13 @@ describe('requests', function () {
.
then
(
function
()
{
expect
(
resolveSpy
).
not
.
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
();
});
...
...
test/unit/adapters/http.js
浏览文件 @
91dae3c4
...
...
@@ -25,8 +25,8 @@ module.exports = {
timeout
:
250
}).
then
(
function
(
res
)
{
success
=
true
;
}).
catch
(
function
(
res
)
{
error
=
res
;
}).
catch
(
function
(
err
)
{
error
=
err
;
failure
=
true
;
});
...
...
@@ -189,8 +189,8 @@ module.exports = {
maxContentLength
:
2000
}).
then
(
function
(
res
)
{
success
=
true
;
}).
catch
(
function
(
res
)
{
error
=
res
;
}).
catch
(
function
(
err
)
{
error
=
err
;
failure
=
true
;
});
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录