feat: Don't reject m.request Promise if extract callback supplied (#2006)
This commit is contained in:
parent
e90f14ebe0
commit
80b6a1af0d
4 changed files with 36 additions and 3 deletions
|
|
@ -88,7 +88,7 @@ module.exports = function($window, Promise) {
|
|||
if (xhr.readyState === 4) {
|
||||
try {
|
||||
var response = (args.extract !== extract) ? args.extract(xhr, args) : args.deserialize(args.extract(xhr, args))
|
||||
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304 || FILE_PROTOCOL_REGEX.test(args.url)) {
|
||||
if (args.extract !== extract || (xhr.status >= 200 && xhr.status < 300) || xhr.status === 304 || FILE_PROTOCOL_REGEX.test(args.url)) {
|
||||
resolve(cast(args.type, response))
|
||||
}
|
||||
else {
|
||||
|
|
|
|||
|
|
@ -519,5 +519,35 @@ o.spec("xhr", function() {
|
|||
o(e instanceof Error).equals(true)
|
||||
}).then(done)
|
||||
})
|
||||
o("does not reject on status error code when extract provided", function(done) {
|
||||
mock.$defineRoutes({
|
||||
"GET /item": function() {
|
||||
return {status: 500, responseText: JSON.stringify({message: "error"})}
|
||||
}
|
||||
})
|
||||
xhr({
|
||||
method: "GET", url: "/item",
|
||||
extract: function(xhr) {return JSON.parse(xhr.responseText)}
|
||||
}).then(function(data) {
|
||||
o(data.message).equals("error")
|
||||
done()
|
||||
})
|
||||
})
|
||||
o("rejects on error in extract", function(done) {
|
||||
mock.$defineRoutes({
|
||||
"GET /item": function() {
|
||||
return {status: 200, responseText: JSON.stringify({a: 1})}
|
||||
}
|
||||
})
|
||||
xhr({
|
||||
method: "GET", url: "/item",
|
||||
extract: function() {throw new Error("error")}
|
||||
}).catch(function(e) {
|
||||
o(e instanceof Error).equals(true)
|
||||
o(e.message).equals("error")
|
||||
}).then(function() {
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue