Model fetch fail callback

Hi Enyo folks,
I have setup success and fail callbacks in a fetch, and the success seems to work great, but when I test with URLs that will generate a 401, 404 or a CORS error, I thought it would go to the fail callback, but it doesn't hit either callback when fetching any of those URLs. I'm starting to feel that there is supposed to be a different way to deal with these errors, but I sure don't know it...
Here's what the fetch looks like:
this.$.setup.myModel.fetch({
  success: enyoUtils.bindSafely(this, 'modelSuccessCallback'), 
  fail: enyoUtils.bindSafely(this, 'modelFailCallback')
});
-Any help appreciated!
Here is a fiddle in case that helps. The successful jsontest.com URL is commented out now with the active fetch URL producing a 404, but not triggering the fail callback:
http://jsfiddle.net/buckbito/4czd9may/

Comments

  • Many Thanks @theryanjduffy !
    That's what I get for trying to 'port' 2.4 code....
    Now I need to figure out how to get the HTTP Status Code, e.g. 404 - I'm poking at it now and starting to think I need to reach into the ajax.error. Is there an 'Enyo Way' to elegantly get that from the model callbacks?
  • Looks like the error callback receives the status as the first arg and the Ajax instance as the second. One of those should be able to get you what you need.
  • edited April 5
    Hey @theryanjduffy , I really appreciate your help! The Model callback args for error are "model, action, opts, res, source".
    model is an object ref,
    action is a String (e.g. "FETCHING"),
    opts is the original Model~Options object,
    res (result) can be anything? - when I force a 404 I get 0 (zero),
    source is a String (name of the model's Source)

    I can dig into the model returned and and get e.g. model.source, but I still can't figure out how to get the ajax.error without making a fetch override along the lines of what @Fred_D_Castillo helped me with here:
    http://forums.enyojs.com/discussion/2535/how-to-hook-into-response-error-when-commiting-through-a-model#latest

    I was imagining that there must be a way to get HTTP status from an enyo/Model without overriding fetch - is there?

    If I could get a reference to the ajax.error from the Model's callback, then I think I'd have the args described in @theryanjduffy 's post above...
  • edited April 6
    The res field should be what you want. A response of 0 usually means some non-resource error a like CORS failure. Here's a quick illustrative fiddle. Would have to dig into your specific scenario a bit more to understand what's happening.

    https://jsfiddle.net/ecw5x2ue/
  • edited April 6
    @theryanjduffy - Huge help! Thanks, I didn't realize that the CORS error was preventing the underlying 404 status to come through. I was looking at the Chrome console which shows the 404 and the CORS error.
    
    GET http://movettefilm.com/fakepage.html 404 (Not Found)
    XMLHttpRequest cannot load http://movettefilm.com/fakepage.html. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access. The response had HTTP status code 404.
    

    I have substituted a URL that returns 404 without CORS ('http://www.jsontest.com/fakefake.html') and now I get it in res
    ERROR: res = 404
    ERROR: JSON.stringify(res) = 404
    Thanks again! I thought there must be a way without mucking around in the Ajax - your response was a huge help!

    Here's the updated fiddle for posterity that logs all the callback args to the console...
    http://jsfiddle.net/buckbito/4czd9may/
Sign In or Register to comment.