File size: 1,672 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { TurbopackInternalErrorOpts } from '../../../build/swc/generated-native'
import { eventErrorThrown } from '../../../telemetry/events'
import { traceGlobals } from '../../../trace/shared'

/**
 * An error caused by a bug in Turbopack, and not the user's code (e.g. a Rust panic). These should
 * be written to a log file and details should not be shown to the user.
 *
 * These are constructed in Turbopack by calling `throwTurbopackInternalError`.
 */
export class TurbopackInternalError extends Error {
  name = 'TurbopackInternalError'
  location: string | undefined

  // Manually set this as this isn't statically determinable
  __NEXT_ERROR_CODE = 'TurbopackInternalError'

  constructor({ message, anonymizedLocation }: TurbopackInternalErrorOpts) {
    super(message)
    this.location = anonymizedLocation
  }
}

/**
 * A helper used by the napi Rust entrypoints to construct and throw a `TurbopackInternalError`.
 *
 * When called, this will emit a telemetry event.
 */
export function throwTurbopackInternalError(
  conversionError: Error | null,
  opts: TurbopackInternalErrorOpts
): never {
  if (conversionError != null) {
    // Somehow napi failed to convert `opts` to a JS object??? Just give up and throw that instead.
    throw new Error(
      'NAPI type conversion error in throwTurbopackInternalError',
      {
        cause: conversionError,
      }
    )
  }
  const err = new TurbopackInternalError(opts)
  const telemetry = traceGlobals.get('telemetry')
  if (telemetry) {
    telemetry.record(eventErrorThrown(err, opts.anonymizedLocation))
  } else {
    console.error('Expected `telemetry` to be set in globals')
  }
  throw err
}