Skip to content

Commit e1305de

Browse files
committed
routes/user: Show error page if user is missing or fails to load
1 parent 9171161 commit e1305de

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

app/routes/user.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class UserRoute extends Route {
1212
sort: { refreshModel: true },
1313
};
1414

15-
async model(params) {
15+
async model(params, transition) {
1616
const { user_id } = params;
1717
try {
1818
let user = await this.store.queryRecord('user', { user_id });
@@ -24,11 +24,12 @@ export default class UserRoute extends Route {
2424
return { crates, user };
2525
} catch (error) {
2626
if (error instanceof NotFoundError) {
27-
this.notifications.error(`User '${params.user_id}' does not exist`);
28-
return this.router.replaceWith('index');
27+
let title = `${user_id}: User not found`;
28+
this.router.replaceWith('catch-all', { transition, error, title });
29+
} else {
30+
let title = `${user_id}: Failed to load user data`;
31+
this.router.replaceWith('catch-all', { transition, error, title, tryAgain: true });
2932
}
30-
31-
throw error;
3233
}
3334
}
3435
}

tests/routes/user-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { currentURL } from '@ember/test-helpers';
2+
import { module, test } from 'qunit';
3+
4+
import { setupApplicationTest } from 'crates-io/tests/helpers';
5+
6+
import { visit } from '../helpers/visit-ignoring-abort';
7+
8+
module('Route | user', function (hooks) {
9+
setupApplicationTest(hooks);
10+
11+
test("shows an error message if the user can't be found", async function (assert) {
12+
await visit('/users/foo');
13+
assert.strictEqual(currentURL(), '/users/foo');
14+
assert.dom('[data-test-404-page]').exists();
15+
assert.dom('[data-test-title]').hasText('foo: User not found');
16+
assert.dom('[data-test-go-back]').exists();
17+
assert.dom('[data-test-try-again]').doesNotExist();
18+
});
19+
20+
test('server error causes the error page to be shown', async function (assert) {
21+
this.server.get('/api/v1/users/:userId', {}, 500);
22+
23+
await visit('/users/foo');
24+
assert.strictEqual(currentURL(), '/users/foo');
25+
assert.dom('[data-test-404-page]').exists();
26+
assert.dom('[data-test-title]').hasText('foo: Failed to load user data');
27+
assert.dom('[data-test-go-back]').doesNotExist();
28+
assert.dom('[data-test-try-again]').exists();
29+
});
30+
});

0 commit comments

Comments
 (0)