Handle 403s gracefully

- When a list 403s, treat it as if it were empty
- When a single resource 403s, redirect to an application error page
  that has a backdoor link to the tokens page
This commit is contained in:
Michael Lange
2017-10-12 17:40:49 -07:00
parent b8ed3f5664
commit 1cfa5f5ecc
3 changed files with 24 additions and 15 deletions

View File

@@ -1,5 +1,6 @@
import Ember from 'ember';
import RESTAdapter from 'ember-data/adapters/rest';
import codesForError from '../utils/codes-for-error';
const { get, computed, inject } = Ember;
@@ -21,8 +22,12 @@ export default RESTAdapter.extend({
findAll() {
return this._super(...arguments).catch(error => {
if (error.code === '501' || (error.errors && error.errors.findBy('status', '501'))) {
// Feature is not implemented in this version of Nomad
const errorCodes = codesForError(error);
const isNotAuthorized = errorCodes.includes('403');
const isNotImplemented = errorCodes.includes('501');
if (isNotAuthorized || isNotImplemented) {
return [];
}

View File

@@ -1,4 +1,5 @@
import Ember from 'ember';
import codesForError from '../utils/codes-for-error';
const { Controller, computed, inject, run, observer } = Ember;
@@ -12,19 +13,7 @@ export default Controller.extend({
}),
errorCodes: computed('error', function() {
const error = this.get('error');
const codes = [error.code];
if (error.errors) {
error.errors.forEach(err => {
codes.push(err.status);
});
}
return codes
.compact()
.uniq()
.map(code => '' + code);
return codesForError(this.get('error'));
}),
is403: computed('errorCodes.[]', function() {

View File

@@ -0,0 +1,15 @@
// Returns an array of error codes as strings for an Ember error object
export default function codesForError(error) {
const codes = [error.code];
if (error.errors) {
error.errors.forEach(err => {
codes.push(err.status);
});
}
return codes
.compact()
.uniq()
.map(code => '' + code);
}