Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, February 10, 2016

In Ember automatically route application to error route if template promise returns error

In Ember automatically route application to error route if template promise returns error


Given a route it is easy enough to get Ember to navigate to an error route if the model promise rejects. Currently I have the simple method of having an error.hbs and /routes/error.js, and if the model promise errors it loads the error page.

I have a situation where the model resolves fine, but one of its relationships does not. This relationship is used in the routes template e.g.

{{#each model.addresses as |address id|}}    {{address.line1}}  {{/each}}  

So myurl.com/api/addresses/1 returns a 500 error. I see this in the console window, but Ember does not automatically transition to the error page. How can I get it to do this for all promises that reject as a result of a template requesting more data?

Answer by Daniel Kmak for In Ember automatically route application to error route if template promise returns error


One solution would be to return RSVP.Promise in route model() which loads all data (also relationships instead of lazy loading them later by template) and rejects if any of asynchronous requests fail.

Answer by Christian Stengel for In Ember automatically route application to error route if template promise returns error


Ember generates the error pages by it self if im not wrong, if ember depends on an DS.error object for the transition you have to fulfill the requirements in order to get Ember to recognize an valid error, in Ember 2.x the error code MUST be 422 and has to follow jsonapi http://jsonapi.org/format/#errors-processing

Answer by pixelhandler for In Ember automatically route application to error route if template promise returns error


I wrote up a long post on using Route error substates here -> https://pixelhandler.com/posts/emberjs-handling-failure-using-route-error-substates

Answer by Stefan Penner for In Ember automatically route application to error route if template promise returns error


So ultimately, error handling happens as the result of knowing "what broke" and "who owns the fact that it broke". Generally, if X asked for Y and Y fails, its X's job to display the error. In that example, if FooRoute triggers an ajax, and it fails, FooRoute more or less should handle the error.

But when it comes to the template causing fetches, it actually becomes the responsibility of the template.

One thing one can do is, actually bind to the various boolean properties representing the async operation on the PromiseProxy (in your case, model.addresses). The properties are as follows

  • isPending - ongoing work is happening
  • isFulfilled - it is done, and succeeded
  • isRejected - it is done, but failed
  • isSettled, - it is done (failed or succeeded)

using these flags could be as follows

{{#if model.addresses.isPending}}     loading...  {{else if mode.addresses.isRejected}}    Sorry something went wrong, {{model.addresses.reason.message}}  {{else}}    {{#each model.addresses as |address| }}       ...    {{/each}}  {{/if}}  

Answer by James A. Rosen for In Ember automatically route application to error route if template promise returns error


When loading data for a page, I like to ask myself, what data is critical to show? That is, if there are three sections on the page -- say backed by foo, foo.bars, and foo.baz -- are all three sections of highest priority? Or can I let one of them fail and show the other two? Let's assume that without either foo or foo.bars, we show an error page, but foo.baz is optional. In that case, I would write that as

// controller:foo  model(params) {    return this.get('store').find('foo', params.id);  },  afterModel(foo) {    return foo.get('bars'); // block on foo.bars fetch  }  

Then I might have a {{foo-baz}} component that renders Loading... at first, then fetches foo.baz in the background and shows the full content later.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.