From: Ricardo Marques Date: Wed, 23 May 2018 10:09:23 +0000 (+0100) Subject: mgr/dashboard: Log frontend errors in backend X-Git-Tag: v14.0.1~1221^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F22285%2Fhead;p=ceph.git mgr/dashboard: Log frontend errors in backend Signed-off-by: Ricardo Marques --- diff --git a/src/pybind/mgr/dashboard/controllers/logging.py b/src/pybind/mgr/dashboard/controllers/logging.py new file mode 100644 index 00000000000..f92fc2bde3a --- /dev/null +++ b/src/pybind/mgr/dashboard/controllers/logging.py @@ -0,0 +1,10 @@ +from . import UiApiController, BaseController, Endpoint +from .. import logger + + +@UiApiController('/logging') +class Logging(BaseController): + + @Endpoint('POST', path='js-error') + def jsError(self, url, message, stack): + logger.error('frontend error (%s): %s\n %s\n', url, message, stack) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/app.module.ts b/src/pybind/mgr/dashboard/frontend/src/app/app.module.ts index 4c5974ec304..a13d50c8b57 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/app.module.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/app.module.ts @@ -1,5 +1,5 @@ import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http'; -import { NgModule } from '@angular/core'; +import { ErrorHandler, NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; @@ -11,6 +11,7 @@ import { AppComponent } from './app.component'; import { CephModule } from './ceph/ceph.module'; import { CoreModule } from './core/core.module'; import { ApiInterceptorService } from './shared/services/api-interceptor.service'; +import { JsErrorHandler } from './shared/services/js-error-handler.service'; import { SharedModule } from './shared/shared.module'; export class CustomOption extends ToastOptions { @@ -39,6 +40,10 @@ export class CustomOption extends ToastOptions { ], exports: [SharedModule], providers: [ + { + provide: ErrorHandler, + useClass: JsErrorHandler, + }, { provide: HTTP_INTERCEPTORS, useClass: ApiInterceptorService, diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/api.module.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/api.module.ts index 4e63dc17692..8a8fd64da5c 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/api.module.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/api.module.ts @@ -6,6 +6,7 @@ import { CephfsService } from './cephfs.service'; import { ConfigurationService } from './configuration.service'; import { DashboardService } from './dashboard.service'; import { HostService } from './host.service'; +import { LoggingService } from './logging.service'; import { MonitorService } from './monitor.service'; import { OsdService } from './osd.service'; import { PerformanceCounterService } from './performance-counter.service'; @@ -35,6 +36,7 @@ import { TcmuIscsiService } from './tcmu-iscsi.service'; RgwDaemonService, RgwUserService, PerformanceCounterService, + LoggingService, TcmuIscsiService ] }) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/logging.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/logging.service.ts new file mode 100644 index 00000000000..746df543874 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/logging.service.ts @@ -0,0 +1,19 @@ +import { HttpClient } from '@angular/common/http'; +import { Injectable } from '@angular/core'; + +@Injectable() +export class LoggingService { + + constructor(private http: HttpClient) { + } + + jsError(url, message, stack) { + const request = { + url: url, + message: message, + stack: stack + }; + return this.http.post('ui-api/logging/js-error', request); + } + +} diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/js-error-handler.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/js-error-handler.service.ts new file mode 100644 index 00000000000..c906cad31d7 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/js-error-handler.service.ts @@ -0,0 +1,18 @@ +import { ErrorHandler, Injectable, Injector } from '@angular/core'; +import { LoggingService } from '../api/logging.service'; + +@Injectable() +export class JsErrorHandler implements ErrorHandler { + + constructor(private injector: Injector) { } + + handleError(error) { + 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(); + throw error; + } + +}