ensure http errors reject promises

This commit is contained in:
Leo Horie 2014-05-26 21:57:09 -04:00
parent ea0b661b79
commit b9cdd940bc
4 changed files with 18 additions and 19 deletions

View file

@ -14,6 +14,7 @@
- Throwing exceptions within `m.request` now follow the same resolution procedure as `m.deferred` [#86](https://github.com/lhorie/mithril.js/issues/85) - Throwing exceptions within `m.request` now follow the same resolution procedure as `m.deferred` [#86](https://github.com/lhorie/mithril.js/issues/85)
- Promises now always update their `m.prop` on success (and leave the m.prop alone on error) - Promises now always update their `m.prop` on success (and leave the m.prop alone on error)
- Nested arrays no longer cause double removal of elements [#87](https://github.com/lhorie/mithril.js/issues/87) - Nested arrays no longer cause double removal of elements [#87](https://github.com/lhorie/mithril.js/issues/87)
- HTTP error codes now correctly reject promises
--- ---

View file

@ -162,7 +162,10 @@ Mithril = m = new function app(window) {
return cachedAttrs return cachedAttrs
} }
function clear(nodes) { function clear(nodes) {
for (var i = nodes.length - 1; i > -1; i--) nodes[i].parentNode.removeChild(nodes[i]) if (nodes.length > 0) {
var parent = nodes[i].parentNode
for (var i = nodes.length - 1; i > -1; i--) parent.removeChild(nodes[i])
}
nodes.length = 0 nodes.length = 0
} }
function injectHTML(parentElement, index, data) { function injectHTML(parentElement, index, data) {
@ -450,11 +453,10 @@ Mithril = m = new function app(window) {
function ajax(options) { function ajax(options) {
var xhr = window.XDomainRequest ? new window.XDomainRequest : new window.XMLHttpRequest var xhr = window.XDomainRequest ? new window.XDomainRequest : new window.XMLHttpRequest
xhr.open(options.method, options.url, true, options.user, options.password) xhr.open(options.method, options.url, true, options.user, options.password)
xhr.onload = typeof options.onload == "function" ? options.onload : function() {}
xhr.onerror = typeof options.onerror == "function" ? options.onerror : function() {}
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 0) { if (xhr.readyState === 4) {
xhr.onerror({type: "error", target: xhr}) if (xhr.status >= 200 && xhr.status < 300) options.onload({type: "load", target: xhr})
else options.onerror({type: "error", target: xhr})
} }
} }
if (typeof options.config == "function") options.config(xhr, options) if (typeof options.config == "function") options.config(xhr, options)

View file

@ -678,40 +678,34 @@ function testMithril(mock) {
//m.request //m.request
test(function() { test(function() {
var prop = m.request({method: "GET", url: "test"}) var prop = m.request({method: "GET", url: "test"})
var e = mock.XMLHttpRequest.$events.pop() mock.XMLHttpRequest.$instances.pop().onreadystatechange()
e.target.onload(e)
return prop().method === "GET" && prop().url === "test" return prop().method === "GET" && prop().url === "test"
}) })
test(function() { test(function() {
var prop = m.request({method: "GET", url: "test"}).then(function(value) {return "foo"}) var prop = m.request({method: "GET", url: "test"}).then(function(value) {return "foo"})
var e = mock.XMLHttpRequest.$events.pop() mock.XMLHttpRequest.$instances.pop().onreadystatechange()
e.target.onload(e)
return prop() === "foo" return prop() === "foo"
}) })
test(function() { test(function() {
var prop = m.request({method: "POST", url: "http://domain.com:80", data: {}}).then(function(value) {return value}) var prop = m.request({method: "POST", url: "http://domain.com:80", data: {}}).then(function(value) {return value})
var e = mock.XMLHttpRequest.$events.pop() mock.XMLHttpRequest.$instances.pop().onreadystatechange()
e.target.onload(e)
return prop().url === "http://domain.com:80" return prop().url === "http://domain.com:80"
}) })
test(function() { test(function() {
var prop = m.request({method: "POST", url: "http://domain.com:80/:test1", data: {test1: "foo"}}).then(function(value) {return value}) var prop = m.request({method: "POST", url: "http://domain.com:80/:test1", data: {test1: "foo"}}).then(function(value) {return value})
var e = mock.XMLHttpRequest.$events.pop() mock.XMLHttpRequest.$instances.pop().onreadystatechange()
e.target.onload(e)
return prop().url === "http://domain.com:80/foo" return prop().url === "http://domain.com:80/foo"
}) })
test(function() { test(function() {
var error = m.prop("no error") var error = m.prop("no error")
var prop = m.request({method: "GET", url: "test", deserialize: function() {throw new Error("error occurred")}}).then(null, error) var prop = m.request({method: "GET", url: "test", deserialize: function() {throw new Error("error occurred")}}).then(null, error)
var e = mock.XMLHttpRequest.$events.pop() mock.XMLHttpRequest.$instances.pop().onreadystatechange()
e.target.onload(e)
return prop() === undefined && error().message === "error occurred" return prop() === undefined && error().message === "error occurred"
}) })
test(function() { test(function() {
var error = m.prop("no error"), exception var error = m.prop("no error"), exception
var prop = m.request({method: "GET", url: "test", deserialize: function() {throw new SyntaxError("error occurred")}}).then(null, error) var prop = m.request({method: "GET", url: "test", deserialize: function() {throw new SyntaxError("error occurred")}}).then(null, error)
var event = mock.XMLHttpRequest.$events.pop() try {mock.XMLHttpRequest.$instances.pop().onreadystatechange()}
try {event.target.onload(event)}
catch (e) {exception = e} catch (e) {exception = e}
m.endComputation() m.endComputation()
return prop() === undefined && error() === "no error" && exception.message == "error occurred" return prop() === undefined && error() === "no error" && exception.message == "error occurred"

View file

@ -88,10 +88,12 @@ mock.window = new function() {
} }
this.send = function() { this.send = function() {
this.responseText = JSON.stringify(this) this.responseText = JSON.stringify(this)
request.$events.push({type: "load", target: this}) this.readyState = 4
this.status = 200
request.$instances.push(this)
} }
} }
request.$events = [] request.$instances = []
return request return request
} }
window.location = {search: "", pathname: "", hash: ""}, window.location = {search: "", pathname: "", hash: ""},