github.com/outbrain/consul@v1.4.5/ui-v2/app/routes/application.js (about)

     1  import Route from '@ember/routing/route';
     2  import { inject as service } from '@ember/service';
     3  import { hash } from 'rsvp';
     4  import { get } from '@ember/object';
     5  import { next } from '@ember/runloop';
     6  import { Promise } from 'rsvp';
     7  import WithBlockingActions from 'consul-ui/mixins/with-blocking-actions';
     8  const $html = document.documentElement;
     9  const removeLoading = function() {
    10    return $html.classList.remove('ember-loading');
    11  };
    12  export default Route.extend(WithBlockingActions, {
    13    init: function() {
    14      this._super(...arguments);
    15    },
    16    repo: service('repository/dc'),
    17    settings: service('settings'),
    18    actions: {
    19      loading: function(transition, originRoute) {
    20        let dc = null;
    21        if (originRoute.routeName !== 'dc') {
    22          const model = this.modelFor('dc') || { dcs: null, dc: { Name: null } };
    23          dc = get(this, 'repo').getActive(model.dc.Name, model.dcs);
    24        }
    25        hash({
    26          loading: !$html.classList.contains('ember-loading'),
    27          dc: dc,
    28        }).then(model => {
    29          next(() => {
    30            const controller = this.controllerFor('application');
    31            controller.setProperties(model);
    32            transition.promise.finally(function() {
    33              removeLoading();
    34              controller.setProperties({
    35                loading: false,
    36                dc: model.dc,
    37              });
    38            });
    39          });
    40        });
    41        return true;
    42      },
    43      error: function(e, transition) {
    44        // TODO: Normalize all this better
    45        let error = {
    46          status: e.code || '',
    47          message: e.message || e.detail || 'Error',
    48        };
    49        if (e.errors && e.errors[0]) {
    50          error = e.errors[0];
    51          error.message = error.title || error.detail || 'Error';
    52        }
    53        // TODO: Unfortunately ember will not maintain the correct URL
    54        // for you i.e. when this happens the URL in your browser location bar
    55        // will be the URL where you clicked on the link to come here
    56        // not the URL where you got the 403 response
    57        // Currently this is dealt with a lot better with the new ACLs system, in that
    58        // if you get a 403 in the ACLs area, the URL is correct
    59        // Moving that app wide right now wouldn't be ideal, therefore simply redirect
    60        // to the ACLs URL instead of maintaining the actual URL, which is better than the old
    61        // 403 page
    62        // To note: Consul only gives you back a 403 if a non-existent token has been sent in the header
    63        // if a token has not been sent at all, it just gives you a 200 with an empty dataset
    64        const model = this.modelFor('dc');
    65        if (error.status === '403') {
    66          return get(this, 'feedback').execute(() => {
    67            return get(this, 'settings')
    68              .delete('token')
    69              .then(() => {
    70                return Promise.reject(this.transitionTo('dc.acls.tokens', model.dc.Name));
    71              });
    72          }, 'authorize');
    73        }
    74        if (error.status === '') {
    75          error.message = 'Error';
    76        }
    77        hash({
    78          error: error,
    79          dc:
    80            error.status.toString().indexOf('5') !== 0
    81              ? get(this, 'repo').getActive()
    82              : model && model.dc
    83                ? model.dc
    84                : { Name: 'Error' },
    85          dcs: model && model.dcs ? model.dcs : [],
    86        })
    87          .then(model => {
    88            removeLoading();
    89            next(() => {
    90              this.controllerFor('error').setProperties(model);
    91            });
    92          })
    93          .catch(e => {
    94            removeLoading();
    95            next(() => {
    96              this.controllerFor('error').setProperties({ error: error });
    97            });
    98          });
    99        return true;
   100      },
   101    },
   102  });