errorRegister.js 1.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* @flow */
import type { StackFrame } from './stack-frame';
import { parse } from './parser';
import { map } from './mapper';
import { unmap } from './unmapper';

type ErrorRecord = {
  error: Error,
  unhandledRejection: boolean,
  contextSize: number,
  enhancedFrames: StackFrame[],
};
type ErrorRecordReference = number;
const recorded: ErrorRecord[] = [];

let errorsConsumed: ErrorRecordReference = 0;

function consume(
  error: Error,
  unhandledRejection: boolean = false,
  contextSize: number = 3
22
): Promise<ErrorRecordReference | null> {
23
24
25
26
27
28
29
30
31
32
33
34
35
  const parsedFrames = parse(error);
  let enhancedFramesPromise;
  if (error.__unmap_source) {
    enhancedFramesPromise = unmap(
      // $FlowFixMe
      error.__unmap_source,
      parsedFrames,
      contextSize
    );
  } else {
    enhancedFramesPromise = map(parsedFrames, contextSize);
  }
  return enhancedFramesPromise.then(enhancedFrames => {
36
37
38
39
40
41
42
    if (
      enhancedFrames
        .map(f => f._originalFileName)
        .filter(f => f != null && f.indexOf('node_modules') === -1).length === 0
    ) {
      return null;
    }
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    enhancedFrames = enhancedFrames.filter(
      ({ functionName }) =>
        functionName == null ||
        functionName.indexOf('__stack_frame_overlay_proxy_console__') === -1
    );
    recorded[++errorsConsumed] = {
      error,
      unhandledRejection,
      contextSize,
      enhancedFrames,
    };
    return errorsConsumed;
  });
}

function getErrorRecord(ref: ErrorRecordReference): ErrorRecord {
  return recorded[ref];
}

function drain() {
  // $FlowFixMe
  const keys = Object.keys(recorded);
  for (let index = 0; index < keys.length; ++index) {
    delete recorded[keys[index]];
  }
}

export { consume, getErrorRecord, drain };
export type { ErrorRecordReference };