All files / server/services/examples examples-error.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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    134x                             74x   74x   74x 74x 74x         41x        
import type { ExamplesErrorCode } from "@shared/examples";
 
const STATUS_BY_CODE: Record<ExamplesErrorCode, number> = {
  INVALID_SELECTION: 400,
  INVALID_REF: 400,
  INVALID_REVISION: 400,
  INVALID_SNAPSHOT: 422,
  SOURCE_UNAVAILABLE: 503,
  EXAMPLE_NOT_FOUND: 404,
  RATE_LIMITED: 429,
  LOAD_CAPACITY_EXCEEDED: 503,
};
 
export class ExamplesError extends Error {
  readonly status: number;
 
  constructor(
    readonly code: ExamplesErrorCode,
    message: string,
    readonly retryAfterSeconds?: number,
  ) {
    super(message);
    this.name = "ExamplesError";
    this.status = STATUS_BY_CODE[code];
  }
}
 
export function asExamplesError(error: unknown): ExamplesError {
  return error instanceof ExamplesError
    ? error
    : new ExamplesError("SOURCE_UNAVAILABLE", "External examples source is unavailable");
}