File size: 2,138 Bytes
b91e262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
import { getDigestForWellKnownError } from './create-error-handler'
import { isReactLargeShellError } from './react-large-shell-error'

export enum Phase {
  ProspectiveRender = 'the prospective render',
  SegmentCollection = 'segment collection',
}

export function printDebugThrownValueForProspectiveRender(
  thrownValue: unknown,
  route: string,
  phase: Phase
) {
  // We don't need to print well-known Next.js errors.
  if (getDigestForWellKnownError(thrownValue)) {
    return
  }

  if (isReactLargeShellError(thrownValue)) {
    // TODO: Aggregate
    console.error(thrownValue)
    return undefined
  }

  let message: undefined | string
  if (
    typeof thrownValue === 'object' &&
    thrownValue !== null &&
    typeof (thrownValue as any).message === 'string'
  ) {
    message = (thrownValue as any).message
    if (typeof (thrownValue as any).stack === 'string') {
      const originalErrorStack: string = (thrownValue as any).stack
      const stackStart = originalErrorStack.indexOf('\n')
      if (stackStart > -1) {
        const error = new Error(
          `Route ${route} errored during ${phase}. These errors are normally ignored and may not prevent the route from prerendering but are logged here because build debugging is enabled.
          
Original Error: ${message}`
        )
        error.stack =
          'Error: ' + error.message + originalErrorStack.slice(stackStart)
        console.error(error)
        return
      }
    }
  } else if (typeof thrownValue === 'string') {
    message = thrownValue
  }

  if (message) {
    console.error(`Route ${route} errored during ${phase}. These errors are normally ignored and may not prevent the route from prerendering but are logged here because build debugging is enabled. No stack was provided.
          
Original Message: ${message}`)
    return
  }

  console.error(
    `Route ${route} errored during ${phase}. These errors are normally ignored and may not prevent the route from prerendering but are logged here because build debugging is enabled. The thrown value is logged just following this message`
  )
  console.error(thrownValue)
  return
}