aboutsummaryrefslogtreecommitdiff
path: root/scripts/apidocs/processing/error.ts
blob: e4117301842a0d10e30925fa39b16a4730763f49 (plain)
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
import { FakerError } from '../../../src/errors/faker-error';
import { CI_PREFLIGHT } from '../../env';
import type { SourceableNode } from './source';
import { getSourcePath } from './source';

export class FakerApiDocsProcessingError extends FakerError {
  constructor(options: {
    type: string;
    name: string;
    source: SourceableNode;
    cause: unknown;
  }) {
    const { type, name, source, cause } = options;

    const mainText = `Failed to process ${type} '${name}'`;
    const causeText = cause instanceof Error ? cause.message : '';
    const { filePath, line, column } = getSourcePath(source);
    const sourceText = `${filePath}:${line}:${column}`;

    if (CI_PREFLIGHT) {
      const sourceArgs = `file=${filePath},line=${line},col=${column}`;
      console.log(`::error ${sourceArgs}::${mainText}: ${causeText}`);
    }

    super(`${mainText} at ${sourceText} : ${causeText}`, {
      cause,
    });
  }
}

export function newProcessingError(options: {
  type: string;
  name: string;
  source: SourceableNode;
  cause: unknown;
}): FakerApiDocsProcessingError {
  const { cause } = options;

  if (cause instanceof FakerApiDocsProcessingError) {
    return cause;
  }

  return new FakerApiDocsProcessingError(options);
}