Skip to content

Show generic flash message if crate data fails to load #3442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/routes/crate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ export default class CrateRoute extends Route {
} catch (error) {
if (error.errors?.some(e => e.detail === 'Not Found')) {
this.notifications.error(`Crate '${params.crate_id}' does not exist`);
this.replaceWith('index');
} else {
throw error;
this.notifications.error(`Loading data for the '${params.crate_id}' crate failed. Please try again later!`);
}

this.replaceWith('index');
}
}

Expand Down
10 changes: 9 additions & 1 deletion app/routes/crate/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ export default class VersionRoute extends Route {

async model(params) {
let crate = this.modelFor('crate');
let versions = await crate.get('versions');

let versions;
try {
versions = await crate.get('versions');
} catch {
this.notifications.error(`Loading data for the '${crate.name}' crate failed. Please try again later!`);
this.replaceWith('index');
return;
}

let version;
let requestedVersion = params.version_num;
Expand Down
34 changes: 33 additions & 1 deletion tests/acceptance/crate-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { click, currentRouteName, currentURL, visit, waitFor } from '@ember/test-helpers';
import { click, currentRouteName, currentURL, waitFor } from '@ember/test-helpers';
import { module, skip, test } from 'qunit';

import percySnapshot from '@percy/ember';
Expand All @@ -8,6 +8,7 @@ import { getPageTitle } from 'ember-page-title/test-support';
import { setupApplicationTest } from 'cargo/tests/helpers';

import axeConfig from '../axe-config';
import { visit } from '../helpers/visit-ignoring-abort';

module('Acceptance | crate page', function (hooks) {
setupApplicationTest(hooks);
Expand Down Expand Up @@ -80,6 +81,22 @@ module('Acceptance | crate page', function (hooks) {
await a11yAudit(axeConfig);
});

test('unknown crate shows an error message', async function (assert) {
await visit('/crates/nanomsg');
assert.equal(currentURL(), '/');
assert.dom('[data-test-notification-message]').hasText("Crate 'nanomsg' does not exist");
});

test('other crate loading error shows an error message', async function (assert) {
this.server.get('/api/v1/crates/:crate_name', {}, 500);

await visit('/crates/nanomsg');
assert.equal(currentURL(), '/');
assert
.dom('[data-test-notification-message]')
.hasText("Loading data for the 'nanomsg' crate failed. Please try again later!");
});

test('unknown versions fall back to latest version and show an error message', async function (assert) {
this.server.create('crate', { name: 'nanomsg' });
this.server.create('version', { crateId: 'nanomsg', num: '0.6.0' });
Expand All @@ -93,6 +110,21 @@ module('Acceptance | crate page', function (hooks) {
assert.dom('[data-test-notification-message]').hasText("Version '0.7.0' of crate 'nanomsg' does not exist");
});

test('other versions loading error shows an error message', async function (assert) {
this.server.create('crate', { name: 'nanomsg' });
this.server.create('version', { crateId: 'nanomsg', num: '0.6.0' });
this.server.create('version', { crateId: 'nanomsg', num: '0.6.1' });

this.server.get('/api/v1/crates/:crate_name/versions', {}, 500);

await visit('/');
await click('[data-test-just-updated] [data-test-crate-link="0"]');
assert.equal(currentURL(), '/');
assert
.dom('[data-test-notification-message]')
.hasText("Loading data for the 'nanomsg' crate failed. Please try again later!");
});

test('navigating to the all versions page', async function (assert) {
this.server.loadFixtures();

Expand Down