aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/GlobalErrorStore.js
blob: 90bf751c32e1810986b7f64a121201421f25c2fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { observable, action } from 'mobx';
import Store from './lib/Store';
import Request from './lib/Request';

export default class GlobalErrorStore extends Store {
  @observable error = null;

  @observable response = {};

  constructor(...args) {
    super(...args);

    Request.registerHook(this._handleRequests);
  }

  _handleRequests = action(async (request) => {
    if (request.isError) {
      this.error = request.error;

      if (request.error.json) {
        this.response = await request.error.json();

        if (this.error.status === 401) {
          this.actions.user.logout({ serverLogout: true });
        }
      }
    }
  });
}