File size: 1,105 Bytes
f55c62a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export type DbErrorKind =
  | 'unique-violation'
  | 'fk-violation'
  | 'not-found'
  | 'connection'
  | 'unknown';

export class DbError extends Error {
  readonly kind: DbErrorKind;
  readonly cause?: unknown;

  constructor(kind: DbErrorKind, message: string, cause?: unknown) {
    super(message);
    this.name = 'DbError';
    this.kind = kind;
    this.cause = cause;
  }
}

export function classifyPgError(err: unknown): DbErrorKind {
  const code = (err as { code?: string } | undefined)?.code;
  if (code === '23505') return 'unique-violation';
  if (code === '23503') return 'fk-violation';
  if (
    code === 'ECONNREFUSED' ||
    code === 'ECONNRESET' ||
    code === 'ETIMEDOUT'
  ) {
    return 'connection';
  }
  return 'unknown';
}

export function classifySqliteError(err: unknown): DbErrorKind {
  const code = (err as { code?: string } | undefined)?.code;
  if (
    code === 'SQLITE_CONSTRAINT_UNIQUE' ||
    code === 'SQLITE_CONSTRAINT_PRIMARYKEY'
  ) {
    return 'unique-violation';
  }
  if (code === 'SQLITE_CONSTRAINT_FOREIGNKEY') return 'fk-violation';
  return 'unknown';
}