Code coverage report for node/src/log/stackdriver.ts

Statements: 0% (0 / 14)      Branches: 0% (0 / 9)      Functions: 0% (0 / 4)      Lines: 0% (0 / 14)      Ignored: none     

All files » node/src/log/ » stackdriver.ts
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                                                                                                               
import {ErrorReporting} from '@google-cloud/error-reporting';
import {Providers, Message, LogLevel, Interceptor} from '@btilford/ts-base-core';
import {ErrorMessage} from '@google-cloud/error-reporting/build/src/classes/error-message';
 
import {internal} from '@btilford/ts-base-core';
import {logLevelFilter} from '../../../core/src/log';
 
 
export function registerErrorReporting(errors?: ErrorReporting): ErrorReporting {
  if (!errors) {
    errors = new ErrorReporting();
  }
  return Providers.register(errors, ErrorReporting);
}
 
 
export interface ErrorBuilder {
  (error: ErrorMessage, context: Message): ErrorMessage;
}
 
 
export type StackdriverInterceptorOptions = {
  errors?: ErrorReporting;
  errorBuilder: ErrorBuilder;
  filter: (context: Message) => boolean;
 
}
 
export const DefaultStackdriverInterceptorOptions: StackdriverInterceptorOptions = {
  errorBuilder: (event: ErrorMessage, context: Message) => {
    event.setMessage(context.message);
 
    return event;
  },
  filter: logLevelFilter(LogLevel.ALERT),
};
 
 
export function stackdriverInterceptor(options: StackdriverInterceptorOptions = DefaultStackdriverInterceptorOptions): Interceptor {
  const log = internal.logger('StackdriverInterceptor');
  return function forwardErrors(context: Message): void {
    const errors: ErrorReporting = options.errors || Providers.provide(ErrorReporting);
    if (errors) {
      if (options.filter(context)) {
        const errorEvent = options.errorBuilder(errors.event(), context);
 
        errors.report(errorEvent);
 
      }
    }
    else {
      log.warn('Unable to forward event to Stackdriver! No ErrorReporting provider available!');
    }
  }
}