All files / src/tests karmaErrorParser.ts

0% Statements 0/17
0% Branches 0/7
0% Functions 0/2
0% Lines 0/16

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 34 35 36                                                                       
import {
  ConflictError,
  InternalError,
  NotFoundError,
  SerializationError,
  ValidationError,
} from '@decaf-ts/db-decorators';
 
export function runAndParseError(func: () => any) {
  try {
    return func();
  } catch (e: unknown) {
    throw parseKarmaError(e as Error);
  }
}
 
export function parseKarmaError(e: Error) {
  const regexp = /^\[(.+?)\](.+)$/gs;
  const m = regexp.exec(e.message);
  Iif (!m) return e;
  switch (m[1]) {
    case ConflictError.name:
      return new ConflictError(m[2]);
    case InternalError.name:
      return new InternalError(m[2]);
    case NotFoundError.name:
      return new NotFoundError(m[2]);
    case SerializationError.name:
      return new SerializationError(m[2]);
    case ValidationError.name:
      return new ValidationError(m[2]);
    default:
      return e;
  }
}