Skip to content

Commit 43a5d77

Browse files
committed
Auto merge of #4481 - Turbo87:keyword-error, r=Turbo87
keyword: Show error page if data loading fails ... or empty list if no crates match or keyword does not exist
2 parents 7a845ad + 8f62c4c commit 43a5d77

File tree

7 files changed

+30
-47
lines changed

7 files changed

+30
-47
lines changed

app/controllers/keyword/index.js renamed to app/controllers/keyword.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { tracked } from '@glimmer/tracking';
33

44
import { reads } from 'macro-decorators';
55

6-
import { pagination } from '../../utils/pagination';
6+
import { pagination } from '../utils/pagination';
77

88
export default class KeywordIndexController extends Controller {
99
queryParams = ['page', 'per_page', 'sort'];
1010
@tracked page = '1';
1111
@tracked per_page = 10;
1212
@tracked sort = 'recent-downloads';
1313

14-
@reads('model.meta.total') totalItems;
14+
@reads('model.crates.meta.total') totalItems;
1515

1616
@pagination() pagination;
1717

app/routes/keyword.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
import { NotFoundError } from '@ember-data/adapter/error';
21
import Route from '@ember/routing/route';
32
import { inject as service } from '@ember/service';
43

5-
export default class KeywordRoute extends Route {
6-
@service notifications;
4+
export default class KeywordIndexRoute extends Route {
75
@service router;
86
@service store;
97

10-
async model({ keyword_id }) {
8+
queryParams = {
9+
page: { refreshModel: true },
10+
sort: { refreshModel: true },
11+
};
12+
13+
async model(params, transition) {
14+
let { keyword_id: keyword, page, sort } = params;
15+
1116
try {
12-
return await this.store.find('keyword', keyword_id);
17+
let crates = await this.store.query('crate', { keyword, page, sort });
18+
return { keyword, crates };
1319
} catch (error) {
14-
if (error instanceof NotFoundError) {
15-
this.notifications.error(`Keyword '${keyword_id}' does not exist`);
16-
return this.router.replaceWith('index');
17-
}
18-
19-
throw error;
20+
let title = `${keyword}: Failed to load crates`;
21+
this.router.replaceWith('catch-all', { transition, error, title, tryAgain: true });
2022
}
2123
}
2224
}

app/routes/keyword/index.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

app/templates/index.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165
{{#each this.model.popular_keywords as |keyword|}}
166166
<li>
167167
<FrontPageList::Item
168-
@link={{link "keyword" keyword}}
168+
@link={{link "keyword" keyword.id}}
169169
@title={{keyword.id}}
170170
@subtitle="{{format-num keyword.crates_cnt}} crates"
171171
/>

app/templates/keyword/index.hbs renamed to app/templates/keyword.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
{{page-title this.keyword.keyword ' - Keywords'}}
1+
{{page-title @model.keyword ' - Keywords'}}
22

3-
<PageHeader @title="All Crates" @suffix="for keyword '{{this.keyword.keyword}}'"/>
3+
<PageHeader @title="All Crates" @suffix="for keyword '{{@model.keyword}}'"/>
44

55
<div local-class="results-meta">
66
<ResultsCount
@@ -22,6 +22,6 @@
2222
</div>
2323
</div>
2424

25-
<CrateList @crates={{this.model}} local-class="list" />
25+
<CrateList @crates={{@model.crates}} local-class="list" />
2626

2727
<Pagination @pagination={{this.pagination}} />

app/templates/keyword/error.hbs

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/routes/keyword-test.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ import { visit } from '../helpers/visit-ignoring-abort';
88
module('Route | keyword', function (hooks) {
99
setupApplicationTest(hooks);
1010

11-
test("shows an error message if the keyword can't be found", async function (assert) {
12-
await visit('/keywords/unknown');
13-
assert.equal(currentURL(), '/');
14-
assert.dom('[data-test-notification-message]').hasText("Keyword 'unknown' does not exist");
11+
test('shows an empty list if the keyword does not exist on the server', async function (assert) {
12+
await visit('/keywords/foo');
13+
assert.equal(currentURL(), '/keywords/foo');
14+
assert.dom('[data-test-crate-row]').doesNotExist();
1515
});
1616

1717
test('server error causes the error page to be shown', async function (assert) {
18-
this.server.get('/api/v1/keywords/:keywordId', {}, 500);
18+
this.server.get('/api/v1/crates', {}, 500);
1919

20-
await visit('/keywords/error');
21-
assert.equal(currentURL(), '/keywords/error');
22-
assert.dom('[data-test-error-message]').includesText('GET /api/v1/keywords/error returned a 500');
20+
await visit('/keywords/foo');
21+
assert.equal(currentURL(), '/keywords/foo');
22+
assert.dom('[data-test-404-page]').exists();
23+
assert.dom('[data-test-title]').hasText('foo: Failed to load crates');
24+
assert.dom('[data-test-go-back]').doesNotExist();
25+
assert.dom('[data-test-try-again]').exists();
2326
});
2427
});

0 commit comments

Comments
 (0)