<commit_before>import { IError } from '../models/IError';
import { IErrorParser } from 'IErrorParser';
import { IStackFrame } from '../models/IStackFrame';
import { EventPluginContext } from '../plugins/EventPluginContext';

export class WebErrorParser implements IErrorParser {
  public parse(context:EventPluginContext, exception:Error): Promise<IError> {
    return StackTrace.fromError(exception).then(
      (stackFrames: StackTrace.StackFrame[]) => this.processError(context, exception, stackFrames),
      () => this.onParseError(context)
    );
  }

  private processError(context:EventPluginContext, exception:Error, stackFrames: StackTrace.StackFrame[]): Promise<any> {
    var error:IError = {
      message: exception.message,
      stack_trace: this.getStackFrames(context, stackFrames || [])
    };

    context.event.data['@error'] = error;

    return Promise.resolve();
  }

  private onParseError(context:EventPluginContext): Promise<any>  {
    context.cancel = true;
    return Promise.reject(new Error('Unable to parse the exceptions stack trace. This exception will be discarded.'))
  }

  private getStackFrames(context:EventPluginContext, stackFrames:StackTrace.StackFrame[]): IStackFrame[] {
    var frames:IStackFrame[] = [];

    for (var index = 0; index < stackFrames.length; index++) {
      frames.push({
        name: stackFrames[index].functionName,
        parameters: stackFrames[index].args, // TODO: need to verify arguments.
        file_name: stackFrames[index].fileName,
        line_number: stackFrames[index].lineNumber,
        column: stackFrames[index].columnNumber
      });
    }

    return frames;
  }
}<commit_msg>Fixed an issue where no error message was outputted when an error couldn't be parsed.<commit_after>import { IError } from '../models/IError';
import { IErrorParser } from 'IErrorParser';
import { IStackFrame } from '../models/IStackFrame';
import { EventPluginContext } from '../plugins/EventPluginContext';

export class WebErrorParser implements IErrorParser {
  public parse(context:EventPluginContext, exception:Error): Promise<IError> {
    return StackTrace.fromError(exception).then(
      (stackFrames: StackTrace.StackFrame[]) => this.processError(context, exception, stackFrames),
      (error) => this.onParseError(error, context)
    );
  }

  private processError(context:EventPluginContext, exception:Error, stackFrames: StackTrace.StackFrame[]): Promise<any> {
    var error:IError = {
      message: exception.message,
      stack_trace: this.getStackFrames(context, stackFrames || [])
    };

    context.event.data['@error'] = error;

    return Promise.resolve();
  }

  private onParseError(error:Error, context:EventPluginContext): Promise<any>  {
    context.cancel = true;
    var message = 'Unable to parse the exceptions stack trace';
    context.log.error(`${message}: ${error.message}`);
    return Promise.reject(new Error(`${message}. This exception will be discarded.`))
  }

  private getStackFrames(context:EventPluginContext, stackFrames:StackTrace.StackFrame[]): IStackFrame[] {
    var frames:IStackFrame[] = [];

    for (var index = 0; index < stackFrames.length; index++) {
      frames.push({
        name: stackFrames[index].functionName,
        parameters: stackFrames[index].args, // TODO: need to verify arguments.
        file_name: stackFrames[index].fileName,
        line_number: stackFrames[index].lineNumber,
        column: stackFrames[index].columnNumber
      });
    }

    return frames;
  }
}
{'fraction_non_alphanumeric': 0.125, 'fraction_numerical': 0.0005694760820045558, 'mean_word_length': 4.158590308370044, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4715006', 'n_tokens_mistral': 995, 'n_tokens_neox': 951, 'n_words': 222}