File size: 1,576 Bytes
6cda6d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Stable, path-free failures for browser render preparation.
 *
 * The worker stores error.message in the job result, so filesystem errors must never
 * carry absolute paths into that field. The original cause is logged here, on the
 * server, and retained only as a non-serialized Error cause.
 */

import { createLogger } from '../../shared/logging/logger'

const logger = createLogger('BrowserRenderPreparation')

export type BrowserRenderPreparationErrorCode =
  | 'asset-storage'
  | 'invalid-source'
  | 'narration-unavailable'

export class BrowserRenderPreparationError extends Error {
  readonly code: BrowserRenderPreparationErrorCode

  constructor(
    code: BrowserRenderPreparationErrorCode,
    message: string,
    options: { cause?: unknown } = {}
  ) {
    super(message, options.cause === undefined ? undefined : { cause: options.cause })
    this.name = 'BrowserRenderPreparationError'
    this.code = code
  }
}

/**
 * Convert an internal failure into a stable product error while keeping the original
 * cause and its server path in the log only.
 */
export function browserPreparationFailure(
  code: BrowserRenderPreparationErrorCode,
  message: string,
  cause: unknown,
  context: Record<string, unknown> = {}
): BrowserRenderPreparationError {
  if (cause instanceof BrowserRenderPreparationError) {
    return cause
  }
  logger.warn('Browser render preparation failed', {
    ...context,
    code,
    cause: cause instanceof Error ? cause.message : String(cause),
  })
  return new BrowserRenderPreparationError(code, message, { cause })
}