UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

155 lines (153 loc) 4.87 kB
import { of, throwError } from 'rxjs'; import { filter, take } from 'rxjs/operators'; import { Http } from '../data/http'; import { Net } from '../data/net'; import { RpcObservableErrorMonitorClient } from '../rpc/error-monitor/rpc-observable-error-monitor-client'; import { RpcObservableErrorMonitorServer } from '../rpc/error-monitor/rpc-observable-error-monitor-server'; /** * Error Monitor class. */ export class ErrorMonitor { static monitorName = 'ErrorMonitor'; static instance; rpc; rpcObservableErrorMonitorClient; rpcObservableErrorMonitorServer; enabled = false; shellVersionChecked = false; check = () => { }; /** * Gets the current ErrorMonitor instance. */ static get current() { if (ErrorMonitor.instance) { return ErrorMonitor.instance; } ErrorMonitor.instance = new ErrorMonitor(); return ErrorMonitor.instance; } /** * Start and register the check function on shell. * * @param check the check function. */ startOnShell(check) { this.enabled = true; this.check = check; this.updateMonitor(); } /** * Start error monitoring if it was stopped once. */ startOnModule() { this.enabled = true; this.updateMonitor(); } /** * Stop error monitoring if it's started. This works on both shell and module. */ stop() { this.enabled = false; this.check = () => { }; this.updateMonitor(); } /** * Register rpc and start error monitoring. * * @param rpc the rpc. */ registerRpc(rpc) { if (!this.rpc) { this.rpc = rpc; if (MsftSme.isShell()) { rpc.stateChanged .pipe(filter(active => active), take(1)) .subscribe(() => { this.rpcObservableErrorMonitorServer = new RpcObservableErrorMonitorServer(rpc); this.rpcObservableErrorMonitorServer.register(request => { this.check(request); return of(null); }); }); } else { rpc.stateChanged .pipe(filter(active => active), take(1)) .subscribe(() => this.rpcObservableErrorMonitorClient = new RpcObservableErrorMonitorClient(rpc)); } this.enabled = true; this.updateMonitor(); } } /** * Report the error from the ajax error object. * * @param error the ajax error object. */ reportErrorFromAjax(error) { if (!error || !error.request || !error.request.url) { return; } const forbidden = error.xhr && error.xhr.response && error.xhr.response.error && error.xhr.response.error.forbidden; const statusCode = error.status; const request = error.request; const url = request.url; const method = request.method; const message = Net.getErrorMessage(error); let code = null; let action = null; if (error && error.xhr && error.xhr.response && error.xhr.response.error) { code = error.xhr.response.error.code || error.xhr.response.error.errorCode; action = error.xhr.response.error.actionCode; } this.reportError({ httpError: { url, method, statusCode, message, code, action, forbidden } }); } /** * Report the error to shell. * * @param error the error monitor record. */ reportError(error) { if (!this.enabled) { return; } const self = MsftSme.self(); const record = { ...error, ...{ sessionId: self.Init.sessionId, timestamp: Date.now(), moduleName: self.Init.moduleName } }; if (this.rpc && this.rpc.stateActive && this.rpcObservableErrorMonitorClient) { // send to shell. this.rpcObservableErrorMonitorClient.report(record).subscribe(); } else { this.check(record); } } /** * Update the monitoring state. */ updateMonitor() { if (this.enabled) { this.registerHttp(); } else { Http.unregisterMonitors(ErrorMonitor.monitorName); } } /** * Register Http monitoring. */ registerHttp() { Http.registerMonitorSet({ name: ErrorMonitor.monitorName, preMonitor: (request) => of(request), successMonitor: (response) => of(response), errorMonitor: (error) => { this.reportErrorFromAjax(error); return throwError(() => error); } }); } } //# sourceMappingURL=error-monitor.js.map