Question
When mithril's m.request
fails with a timeout or a network error, the error returned is simply null
. This makes it impossible to distinguish between
- timeouts
- a lack of internet connection
- other kinds of errors.
Is there a way to determine this information?
Demo
In the snippet below, the request will timeout every time, since the limit is 1ms. The error returned is null
. If you change it to 1000ms, it will work. If you turn your wifi off, and try again, it will return another null
error.
const Test = {
controller: function() {
var user = m.prop('Waiting...');
var error = m.prop();
var args = {
method: 'GET',
url: "https://jsonplaceholder.typicode.com/users",
config: function(xhr) {
xhr.timeout = 1;
}
};
function refreshUser() {
m.request(args)
.then( users => user(users[0].name), error);
}
return { user, error, refreshUser};
},
view: function(ctrl) {
return m('div', [
m('div', 'Fetched User: ' + ctrl.user()),
m('div', 'Network Error: ' + ctrl.error()),
m('button', {onclick: ctrl.refreshUser }, 'Refresh User')
]);
}
}
m.mount(document.body, Test);
<script src="https://cdnjs.cloudflare.com/ajax/libs/mithril/0.2.5/mithril.min.js"></script>