Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-200 Error Handling Question #38

Closed
jakedowns opened this issue Nov 10, 2017 · 3 comments
Closed

Non-200 Error Handling Question #38

jakedowns opened this issue Nov 10, 2017 · 3 comments

Comments

@jakedowns
Copy link

jakedowns commented Nov 10, 2017

During development, replacing the whole body with any and all server errors is great.
For production, not so great.

Is there a way I could prevent the entire body from being replaced on a failed up.replace call?

So that I may print a simpler, user-friendly error message about the server error? instead of printing exactly what the XHR returned and blowing away the user's entire context / state?

I tried setting the failLayer to auto, page, popup and modal, so that i could just sweep the error under the rug so-to-speak, to give the user a chance to navigate away to something else...

but they all fail with:

[UP] Could not find failure target in current popup (tried ["body"])
--
[UP] Could not find failure target in current modal (tried ["body"])
--
[UP] Could not find failure target in response (tried [".overlay-error-container"])

which breaks JS execution beyond that point, so my .fail handler never gets a chance to display my simplified error message.

@jakedowns
Copy link
Author

jakedowns commented Nov 10, 2017

update: i've wrapped my up.replace call in a try/catch block, and that seems to suffice for now

let options = {
    transition: 'none',
    history: false, // don't update url by default
    //failTarget: '.overlay-error-container', //default 'body'
    //fallback: '.overlay-error-container'
    failLayer: 'invisible-layer' // <- SWEEP NON-200s under the rug
  };

  if(do_update_url){
    options.history = next_history_url;
    options.historyMethod = 'replace';
  }

  return new Promise((resolve, reject)=>{
    try {
      up.replace('#search-results-overlay .inner', load_url, options)
        .then(()=>{
          resolve(); // resolve outer
        })
        // normalize outer promise to native Promise .catch
        // instead of Jquery Deferred .fail
        .fail(()=>{
          reject(); // reject outer

           // SHOW FRIENDLY ERROR, JS FAILS TO REACH THIS BIT
          $('#search-results-overlay .note.error').show();
        })
        .always(()=>{
          this.overlay_fetching_results = false;
        });
    }catch(e){
      reject();

       // SHOW FRIENDLY ERROR, THIS WORKS!
      $('#search-results-overlay .note.error').show();
    }
  });

@jakedowns
Copy link
Author

jakedowns commented Nov 10, 2017

another update: i was mistaken... try/catch did work to show friendly error message, but failLayer: 'bogus-layer-name' breaks up.replace entirely. :<

tried setting up.dom.config.fallbacks = ['.overlay-error-container']; same error though.

  • is there some way i can offload the contents of XHR.body -> into some hidden dom element on the current page?

  • or just override/disable/prevent the default failTarget/failLayer behaviour entirely?

  • maybe the proper unpoly way to solve this would be a server-side try/catch and just always returning 200s & server-rendered friendly error message, when, for example, my search server is overloaded, however I can't always guarantee control of the server response status code at server-application level, and there may still be extreme error examples where I'd still rather not blow away the entire page body due to a server hiccup

@triskweline
Copy link
Contributor

Right now up.replace will always swap a page fragement into up-target, up-fail-target or the first matching selector from up.config.fallbacks.

You cannot prevent this with catch or promise rejection handlers. The rejection handler will be called after up-fail-target was replaced with a non-200 response.

In a future version there might be a way to customize the handling of error responses. Currently the server is expected to always deliver a controlled response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants