Merge pull request #57 from hashicorp/chore-error-messages

Simplify error handling for templates
This commit is contained in:
Susmitha Girumala authored and GitHub committed 2020-06-18 21:32:13 -04:00
commit 1c2020a381
10 files changed
+248 -54

No files matched your search

+49
View File
@@ -4,6 +4,7 @@ import { get } from '@ember/object';
import { InvalidError } from '@ember-data/adapter/error';
import { dasherize } from '@ember/string';
import { pluralize } from 'ember-inflector';
import { isArray } from '@ember/array';
export default class ApplicationAdapter extends RESTAdapter {
// =attributes
@@ -91,6 +92,53 @@ export default class ApplicationAdapter extends RESTAdapter {
return super.handleResponse(status, headers, payload, requestData);
}
/**
* Add convenience booleans to each error within the errors list indicating
* the type of error that occurred. This is useful in downstream templates,
* e.g. `{{if error.isForbidden}}`.
*
* Error booleans are attached based on the error status code:
*
* - `401 isUnauthenticated`: the session isn't authenticated
* - `403 isForbidden`: the session is authenticated but does not have
* permission to perform the requested action
* - `404 isNotFound`: the requested resource could not be found
* - `500 isServer`: an internal server error occurred
* - `isUnknown`: an error occurred, but we don't know which or
* we don't distinguish it yet
*
* @override
* @method normalizeErrorResponse
* @param {Number} status
* @param {Object} headers
* @param {Object} payload
* @return {Array} errors payload
*/
normalizeErrorResponse(/*status, headers, payload*/) {
const errors = super.normalizeErrorResponse(...arguments);
if (isArray(errors)) {
errors.forEach(error => {
switch (Number(error.status)) {
case 401:
error.isUnauthenticated = true;
break;
case 403:
error.isForbidden = true;
break;
case 404:
error.isNotFound = true;
break;
case 500:
error.isServer = true;
break;
default:
error.isUnknown = true;
}
});
}
return errors;
}
/**
* Normalizes the errors found in a payload from our API into JSON API
* error format.
@@ -103,6 +151,7 @@ export default class ApplicationAdapter extends RESTAdapter {
// Normalize the primary error message into "base", which is an error
// that applies to the whole model instance
const baseError = {
isInvalid: true,
status: payload.source,
code: payload.code,
detail: payload.message,
@@ -38,23 +38,37 @@ module('Unit | Adapter | application', function (hooks) {
});
test('it returns an proper InvalidError from handleResponse', function (assert) {
assert.expect(3);
assert.expect(4);
const adapter = this.owner.lookup('adapter:application');
let payload = {
const payload = {
status: 400,
code: 'invalid_argument',
message: 'The request was invalid.',
};
let handledResponse = adapter.handleResponse(400, {}, payload);
const handledResponse = adapter.handleResponse(400, {}, payload);
assert.ok(handledResponse instanceof InvalidError);
assert.equal(handledResponse.errors.length, 1);
assert.equal(handledResponse.message, 'The request was invalid.');
assert.ok(handledResponse.errors[0].isInvalid);
});
test('it assigns convenience booleans for error types', function (assert) {
assert.expect(6);
const adapter = this.owner.lookup('adapter:application');
const getResponse = (status) =>
adapter.handleResponse(status, {}, { status });
assert.ok(getResponse(401).errors[0].isUnauthenticated);
assert.ok(getResponse(403).errors[0].isForbidden);
assert.ok(getResponse(404).errors[0].isNotFound);
assert.ok(getResponse(500).errors[0].isServer);
assert.ok(getResponse(0).errors[0].isUnknown);
assert.ok(getResponse(false).errors[0].isUnknown);
});
test('it returns field-level errors in InvalidError from handleResponse', function (assert) {
assert.expect(1);
const adapter = this.owner.lookup('adapter:application');
let payload = {
const payload = {
status: 400,
code: 'invalid_argument',
message: 'The request was invalid.',
@@ -62,7 +76,7 @@ module('Unit | Adapter | application', function (hooks) {
fields: [{ name: 'name', message: 'Name is wrong.' }],
},
};
let handledResponse = adapter.handleResponse(400, {}, payload);
const handledResponse = adapter.handleResponse(400, {}, payload);
assert.equal(
handledResponse.errors.length,
2,
@@ -3,4 +3,5 @@
.rose-layout-centered {
display: flex;
justify-content: center;
padding: sizing.rems(xl) 0;
}
+2 -6
View File
@@ -7,11 +7,7 @@ export default class OrgsOrgProjectsRoute extends Route {
* Returns all projects from the store.
* @return {Promise[ProjectModel]}
*/
async model() {
try {
return await this.store.findAll('project');
} catch (e) {
return [];
}
model() {
return this.store.findAll('project');
}
}
+58
View File
@@ -0,0 +1,58 @@
{{!--
In an error route, the error instance is `@model`. Error instances contain
a list of errors, each of which has one convenience boolean set on it:
* Error booleans are attached based on the error status code:
*
* - `error.isUnauthenticated`: the session isn't authenticated
* - `error.isForbidden`: the session is authenticated but does not have
* permission to perform the requested action
* - `error.isNotFound`: the requested resource could not be found
* - `error.isServer`: an internal server error occurred
* - `error.isUnknown`: an error occurred, but we don't know which or
* we don't distinguish it yet
--}}
<Rose::Layout::Centered>
{{#each @model.errors as |error|}}
{{!-- error.status === 401 --}}
{{#if error.isUnauthenticated}}
<Rose::Message @title={{t 'errors.titles.unauthenticated-error'}} @subtitle="{{t 'states.error'}} {{error.status}}" @icon="disabled" as |message|>
<message.description>{{t 'errors.descriptions.unauthenticated-error'}}</message.description>
<message.link @route="index">{{t 'actions.help'}}</message.link>
</Rose::Message>
{{/if}}
{{!-- error.status === 403 --}}
{{#if error.isForbidden}}
<Rose::Message @title={{t 'errors.titles.forbidden-error'}} @subtitle="{{t 'states.error'}} {{error.status}}" @icon="disabled" as |message|>
<message.description>{{t 'errors.descriptions.forbidden-error'}}</message.description>
<message.link @route="index">{{t 'actions.help'}}</message.link>
</Rose::Message>
{{/if}}
{{!-- error.status === 404 --}}
{{#if error.isNotFound}}
<Rose::Message @title={{t 'errors.titles.notfound-error'}} @subtitle="{{t 'states.error'}} {{error.status}}" @icon="help-circle-outline" as |message|>
<message.description>{{t 'errors.descriptions.notfound-error'}}</message.description>
<message.link @route="index">{{t 'actions.help'}}</message.link>
</Rose::Message>
{{/if}}
{{!-- error.status === 500 --}}
{{#if error.isServer}}
<Rose::Message @title={{t 'errors.titles.server-error'}} @subtitle="{{t 'states.error'}} {{error.status}}" @icon="alert-circle-outline" as |message|>
<message.description>{{t 'errors.descriptions.server-error'}}</message.description>
<message.link @route="index">{{t 'actions.help'}}</message.link>
</Rose::Message>
{{/if}}
{{!-- error.status === 🤷 everything else --}}
{{#if error.isUnknown}}
<Rose::Message @title={{t 'errors.titles.unknown-error'}} @subtitle="{{t 'states.error'}} {{error.status}}" @icon="alert-circle-outline" as |message|>
<message.description>{{t 'errors.descriptions.unknown-error'}}</message.description>
<message.link @route="index">{{t 'actions.help'}}</message.link>
</Rose::Message>
{{/if}}
{{/each}}
</Rose::Layout::Centered>
@@ -9,21 +9,43 @@
</page.header>
<page.actions>
<Rose::LinkButton @route="orgs.org.projects.new" @style="primary">{{t 'titles.new'}}</Rose::LinkButton>
{{#if @model}}
<Rose::LinkButton @route="orgs.org.projects.new" @style="primary">{{t 'titles.new'}}</Rose::LinkButton>
{{/if}}
</page.actions>
<page.body>
<Rose::Cards as |cards|>
{{#each @model as |project|}}
<cards.link
@title={{project.name}}
@subtitle={{project.id}}
@description={{project.description}}
@route="orgs.org.projects.project"
@model={{project}}
/>
{{/each}}
</Rose::Cards>
{{#if @model}}
<Rose::Cards as |cards|>
{{#each @model as |project|}}
<cards.link
@title={{project.name}}
@subtitle={{project.id}}
@description={{project.description}}
@route="orgs.org.projects.project"
@model={{project}}
/>
{{/each}}
</Rose::Cards>
{{else}}
<Rose::Layout::Centered>
<Rose::Message @title={{t 'titles.projects-welcome'}} as |message|>
<message.description>
{{t 'descriptions.projects-welcome'}}
</message.description>
<message.link @route="orgs.org.projects.new">
<Rose::Icon @name="plus-circle-outline" />
{{t 'titles.new'}}
</message.link>
</Rose::Message>
</Rose::Layout::Centered>
{{/if}}
</page.body>
</Rose::Layout::Page>
@@ -6,42 +6,63 @@
<page.header>
<h2>{{t 'resources.host-catalogs'}}</h2>
<p>{{t 'descriptions.host-catalogs'}}</p>
</page.header>
<page.actions>
<Rose::LinkButton @route="orgs.org.projects.project.host-catalogs.new" @style="primary">
{{t 'titles.new'}}
</Rose::LinkButton>
{{#if @model}}
<Rose::LinkButton @route="orgs.org.projects.project.host-catalogs.new" @style="primary">
{{t 'titles.new'}}
</Rose::LinkButton>
{{/if}}
</page.actions>
<page.body>
<p>{{t 'descriptions.host-catalogs'}}</p>
<Rose::Table as |table|>
<table.header as |header|>
<header.row as |row|>
<row.cell>{{t 'form.label.name'}}</row.cell>
<row.cell>{{t 'form.label.type'}}</row.cell>
<row.cell>{{t 'form.label.description'}}</row.cell>
</header.row>
</table.header>
<table.body as |body|>
{{#each @model as |hostCatalog|}}
<body.row as |row|>
<row.cell>
<LinkTo
@route="orgs.org.projects.project.host-catalogs.host-catalog"
@model={{hostCatalog}}
>
{{hostCatalog.name}}
</LinkTo>
</row.cell>
<row.cell>{{hostCatalog.type}}</row.cell>
<row.cell>{{hostCatalog.description}}</row.cell>
</body.row>
{{/each}}
</table.body>
</Rose::Table>
{{#if @model}}
<Rose::Table as |table|>
<table.header as |header|>
<header.row as |row|>
<row.cell>{{t 'form.label.name'}}</row.cell>
<row.cell>{{t 'form.label.type'}}</row.cell>
<row.cell>{{t 'form.label.description'}}</row.cell>
</header.row>
</table.header>
<table.body as |body|>
{{#each @model as |hostCatalog|}}
<body.row as |row|>
<row.cell>
<LinkTo
@route="orgs.org.projects.project.host-catalogs.host-catalog"
@model={{hostCatalog}}
>
{{hostCatalog.name}}
</LinkTo>
</row.cell>
<row.cell>{{hostCatalog.type}}</row.cell>
<row.cell>{{hostCatalog.description}}</row.cell>
</body.row>
{{/each}}
</table.body>
</Rose::Table>
{{else}}
<Rose::Layout::Centered>
<Rose::Message @title={{t 'titles.host-catalogs-welcome'}} as |message|>
<message.description>
{{t 'descriptions.host-catalogs-welcome'}}
</message.description>
<message.link @route="orgs.org.projects.project.host-catalogs.new">
<Rose::Icon @name="plus-circle-outline" />
{{t 'titles.new'}}
</message.link>
</Rose::Message>
</Rose::Layout::Centered>
{{/if}}
</page.body>
</Rose::Layout::Page>
+13 -1
View File
@@ -1,5 +1,5 @@
import config from '../config/environment';
//import { Response } from 'miragejs';
// import { Response } from 'miragejs';
export default function() {
@@ -48,4 +48,16 @@ export default function() {
this.get('/orgs/:org_id/projects/:project_id/host-catalogs/:id');
this.patch('/orgs/:org_id/projects/:project_id/host-catalogs/:id');
this.del('/orgs/:org_id/projects/:project_id/host-catalogs/:id');
// Uncomment the following line and the Response import above
// Then change the response code to simulate error responses.
// this.get('/orgs/:org_id/projects', () => new Response(505));
// Update error payload to simulate specific error responses.
// this.get('/orgs/:org_id/projects', () => new Response(505, {}, {
// errors: [{
// status: 505,
// message: 'HTTP version not supported.',
// }]
// }));
}
+2 -1
View File
@@ -1,5 +1,6 @@
import { RestSerializer } from 'ember-cli-mirage';
import { underscore } from '@ember/string';
import { isArray } from '@ember/array';
/**
* Manages serialization/normalization of data into and out of the mock API.
@@ -38,7 +39,7 @@ export default RestSerializer.extend({
serialize() {
let json = RestSerializer.prototype.serialize.apply(this, arguments);
// If array, root it under a standard `items` key
if (json.length) json = {items: json};
if (isArray(json)) json = {items: json};
return json;
},
+20
View File
@@ -3,6 +3,7 @@ actions:
delete: Delete
dismiss: Dismiss
save: Save
help: Need help?
resources:
projects: Projects
project: Project
@@ -12,8 +13,14 @@ titles:
new: New
overview: Overview
settings: Settings
empty-set: No Items Yet
projects-welcome: Welcome to Projects
host-catalogs-welcome: Welcome to Host Catalogs
descriptions:
host-catalogs: Overview of collections of hosts and host sets.
empty-set: There are no items to display yet. You may be able to add items or try back later.
projects-welcome: Projects allow you to create and manage host catalogs. To start, create a project.
host-catalogs-welcome: Host catalogs allow you to configure to collect and create hosts and host sets. To start, create a host catalog.
states:
info: Info
success: Success
@@ -24,6 +31,19 @@ form:
name: Name
description: Description
type: Type
errors:
titles:
unauthenticated-error: You are not signed in
forbidden-error: You are not authorized
notfound-error: Resource not found
server-error: Server error
unknown-error: Something went wrong
descriptions:
unauthenticated-error: You are not authenticated. Please authenticate and try again later.
forbidden-error: You must be granted permissions to view this data. Ask your administrator if you think you should have access.
notfound-error: We could not find the requested resource. You can ask your administrator or try again later.
server-error: We ran into a problem and could not continue. You can ask your administrator or try again later.
unknown-error: Please contact your administrator or try again later.
dev:
default-org: Default Org
menu-item: Menu item