From 5379ca241a98e56b2bd87492e80cf67a0d83207e Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Thu, 28 Sep 2017 10:04:33 -0700 Subject: [PATCH] Handle errors in the application route This is the only way to preserve the URL in all cases. --- ui/app/controllers/application.js | 35 +++++++++++++++++++++++++++++++ ui/app/routes/application.js | 10 +++++++++ ui/app/templates/application.hbs | 19 ++++++++++++++++- 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 ui/app/controllers/application.js diff --git a/ui/app/controllers/application.js b/ui/app/controllers/application.js new file mode 100644 index 000000000..0516a7090 --- /dev/null +++ b/ui/app/controllers/application.js @@ -0,0 +1,35 @@ +import Ember from 'ember'; + +const { Controller, computed } = Ember; + +export default Controller.extend({ + error: null, + + errorStr: computed('error', function() { + return this.get('error').toString(); + }), + + errorCodes: computed('error', function() { + const error = this.get('error'); + const codes = [error.code]; + + if (error.errors) { + error.errors.forEach(err => { + codes.push(err.status); + }); + } + + return codes + .compact() + .uniq() + .map(code => '' + code); + }), + + is404: computed('errorCodes.[]', function() { + return this.get('errorCodes').includes('404'); + }), + + is500: computed('errorCodes.[]', function() { + return this.get('errorCodes').includes('500'); + }), +}); diff --git a/ui/app/routes/application.js b/ui/app/routes/application.js index 7197f1819..1b6fe6335 100644 --- a/ui/app/routes/application.js +++ b/ui/app/routes/application.js @@ -3,9 +3,19 @@ import Ember from 'ember'; const { Route } = Ember; export default Route.extend({ + resetController(controller, isExiting) { + if (isExiting) { + controller.set('error', null); + } + }, + actions: { didTransition() { window.scrollTo(0, 0); }, + + error(error) { + this.controllerFor('application').set('error', error); + }, }, }); diff --git a/ui/app/templates/application.hbs b/ui/app/templates/application.hbs index dceb76dfa..0dfba7e6e 100644 --- a/ui/app/templates/application.hbs +++ b/ui/app/templates/application.hbs @@ -1,2 +1,19 @@ {{partial "svg-patterns"}} -{{outlet}} +{{#unless error}} + {{outlet}} +{{else}} +
+
+ {{#if is500}} +

Server Error

+

A server error prevented data from being sent to the client.

+ {{else if is404}} +

Not Found

+

What you're looking for couldn't be found. It either doesn't exist or you are not authorized to see it.

+ {{/if}} + {{#if (eq config.environment "development")}} +
{{errorStr}}
+ {{/if}} +
+
+{{/unless}}