summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/services/js-error-handler.service.ts
blob: de42d005e08285d326d11037f480ce4822c236df (plain)
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
30
31
32
33
import { ErrorHandler, Injectable, Injector } from '@angular/core';
import { Router } from '@angular/router';

import { DashboardError } from '~/app/core/error/error';
import { LoggingService } from '../api/logging.service';

@Injectable()
export class JsErrorHandler implements ErrorHandler {
  constructor(private injector: Injector, private router: Router) {}

  handleError(error: any) {
    const loggingService = this.injector.get(LoggingService);
    const url = window.location.href;
    const message = error && error.message;
    const stack = error && error.stack;
    loggingService.jsError(url, message, stack).subscribe();
    if (error.rejection instanceof DashboardError) {
      setTimeout(
        () =>
          this.router.navigate(['error'], {
            state: {
              message: error.rejection.message,
              header: error.rejection.header,
              icon: error.rejection.icon
            }
          }),
        50
      );
    } else {
      throw error;
    }
  }
}