Skip to content

Commit fd0182f

Browse files
committed
Replace custom error route implementation with ember-error-route
1 parent 193e31b commit fd0182f

File tree

3 files changed

+11
-72
lines changed

3 files changed

+11
-72
lines changed

app/routes/catch-all.js

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,3 @@
1-
import Route from '@ember/routing/route';
2-
import { inject as service } from '@ember/service';
1+
import ErrorRoute from 'ember-error-route';
32

4-
/**
5-
* This is a weird route... but let me explain.
6-
*
7-
* This is the default route that gets used if no other matching route is found.
8-
*
9-
* This route is *also* used as a generic error page via:
10-
*
11-
* ```js
12-
* this.router.replaceWith('catch-all', { transition, title: 'Something failed' });
13-
* ```
14-
*
15-
* Ideally we would use the `error` substate/routes of Ember.js, but those don't
16-
* update the URL when an error happens. This causes the native back button of the
17-
* browser to behave in strange way, so we avoid using the broken built-in error
18-
* routes.
19-
*/
20-
export default class CatchAllRoute extends Route {
21-
@service router;
22-
23-
/**
24-
* If `transitionTo('catch-all', 'foo')` is used, this hook will not get called.
25-
* If the second argument is an object, then the second object will be the `model`
26-
* of this route, and the `serialize()` hook gets called to figure out what the
27-
* URL of this route should be. The URL is automatically assembled from the passed-in
28-
* transition object.
29-
*/
30-
serialize({ transition }) {
31-
return { path: this.pathForRouteInfo(transition.to) };
32-
}
33-
34-
/**
35-
* This internal method takes a `RouteInfo` object from Ember.js (e.g. `transition.to`)
36-
* and returns the corresponding `:path` route parameter for this `catch-all` route.
37-
* @return {string}
38-
*/
39-
pathForRouteInfo(routeInfo) {
40-
let routeName = routeInfo.name;
41-
let params = paramsForRouteInfo(routeInfo);
42-
let queryParams = routeInfo.queryParams;
43-
return this.router.urlFor(routeName, ...params, { queryParams }).slice(1);
44-
}
45-
}
46-
47-
/**
48-
* Returns all route parameters for the passed-in `RouteInfo` object.
49-
*
50-
* These can be used in `router.urlFor(...)` calls.
51-
*/
52-
function paramsForRouteInfo(routeInfo) {
53-
let routeInfos = [...allRouteInfos(routeInfo)].reverse();
54-
55-
let params = [];
56-
for (let routeInfo of routeInfos) {
57-
for (let paramName of routeInfo.paramNames) {
58-
params.push(routeInfo.params[paramName]);
59-
}
60-
}
61-
return params;
62-
}
63-
64-
/**
65-
* Iterates upwards through the `RouteInfo` "family tree" until the top-most
66-
* `RouteInfo` is reached.
67-
*/
68-
function* allRouteInfos(routeInfo) {
69-
yield routeInfo;
70-
while ((routeInfo = routeInfo.parent)) {
71-
yield routeInfo;
72-
}
73-
}
3+
export default class CatchAllRoute extends ErrorRoute {}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"ember-css-modules": "2.0.0",
8989
"ember-data": "3.27.1",
9090
"ember-disable-prototype-extensions": "1.1.3",
91+
"ember-error-route": "0.1.0",
9192
"ember-event-helpers": "0.1.1",
9293
"ember-exam": "7.0.1",
9394
"ember-export-application-global": "2.0.1",

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6776,6 +6776,14 @@ ember-disable-prototype-extensions@1.1.3:
67766776
resolved "https://registry.yarnpkg.com/ember-disable-prototype-extensions/-/ember-disable-prototype-extensions-1.1.3.tgz#1969135217654b5e278f9fe2d9d4e49b5720329e"
67776777
integrity sha1-GWkTUhdlS14nj5/i2dTkm1cgMp4=
67786778

6779+
ember-error-route@0.1.0:
6780+
version "0.1.0"
6781+
resolved "https://registry.yarnpkg.com/ember-error-route/-/ember-error-route-0.1.0.tgz#328fb86e95401590dfeda9e18fc7b963301b0241"
6782+
integrity sha512-0WW3RBRvTs64wjosYP5JrqS3EjT3uM4FemF2Vm3meNdb0W++RJj+tjZqoIZVOkcqVTIzQjJzrCd9e7DJ8wxs5Q==
6783+
dependencies:
6784+
ember-cli-babel "^7.26.6"
6785+
ember-cli-htmlbars "^6.0.0"
6786+
67796787
ember-event-helpers@0.1.1:
67806788
version "0.1.1"
67816789
resolved "https://registry.yarnpkg.com/ember-event-helpers/-/ember-event-helpers-0.1.1.tgz#8df141306c927b9d1a7bc63d63095e92c0ba90a1"

0 commit comments

Comments
 (0)