mardi 25 mai 2021

Catch (failed)net::ERR_NAME_NOT_RESOLVED in an Ember / Ember Data app

I'm working on an Ember app which uses Ember Data. We mainly use Rails and Postgres for the backend, but a smaller portion of our data is stored in a WordPress backend. WordPress is running on wp.example.com.

Ember Data is setup to work with both the Rails and WordPress backends, so I can do things like this:

// Get WordPress category by slug
this.store.query('wordpress/category', { slug }).then((models) => {
  // Leave page if no category was found
  if (typeof models.get('firstObject') == 'undefined') this.transitionTo('backupRoute');

  return models.get('firstObject');
});

Now I'd like to know how i can catch the error if the wp subdomain is offline.

When I change the WordPress backend URL (I'm not sure if that's the best way to simulate a failure), I get (failed)net::ERR_NAME_NOT_RESOLVED in Chrome DevTools after a few seconds and Ember showing error 500. Instead, I'd like to catch the network error and do something useful, in this case, a redirect.

Is there a way to catch those errors? Adding a simple catch() breaks the page completely. It remains white for about two minutes while there's a pending request, then it shows 502 Bad Gateway. I also get this in the logs:

my_service_1 | (node:1) UnhandledPromiseRejectionWarning: [object Object]
my_service_1 | (node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 13)



Aucun commentaire:

Enregistrer un commentaire