Repository navigation
Expand file tree
/
Copy patherror.ts
More file actions
126 lines (117 loc) · 3.32 KB
/
Copy patherror.ts
File metadata and controls
126 lines (117 loc) · 3.32 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
namespace APIErrors {
export interface PgError {
message: {
error: 'postgres query error' | 'query execution failed';
internal?: {
error?: {
message: string;
};
};
code: string;
};
code: string;
}
export interface InternalError {
internal: {
error: {
message: string;
description: string;
};
};
}
export type GeneralError = {
code?: string;
message:
| string
| {
error?: {
message: string;
};
code?: string;
};
};
export const hasField = <T extends string, U = unknown>(
e: unknown,
check: T
): e is Record<T, U> => {
if (typeof e !== 'object' || e === null) return false;
return check in e;
};
export const hasStringField = <T extends string>(
e: unknown,
check: T
): e is Record<T, string> => {
return hasField<T, string>(e, check);
};
export const isPgError = (e: unknown): e is PgError => {
if (!hasField(e, 'message')) return false;
if (!hasField(e.message, 'error')) return false;
const errString = e.message.error;
return (
errString === 'postgres query error' ||
errString === 'query execution failed'
);
};
export const isInternalError = (e: unknown): e is InternalError => {
return hasField(e, 'error') && hasField(e.error, 'internal');
};
export const isGeneralError = (e: unknown): e is GeneralError => {
return hasField(e, 'message');
};
}
export class APIError extends Error {
public name = 'APIError';
constructor(
public message: string,
public code?: string,
public description?: string
) {
super(message);
}
// TODO: write tests for this
static fromUnknown(error: unknown): APIError {
const defaultMessage = 'request failed';
if (typeof error === 'string') return new APIError(error);
if (APIErrors.isPgError(error)) {
if (error.message.internal) {
const message = error.message.internal.error?.message;
return new APIError(message ?? defaultMessage, error.message.code);
}
return new APIError(error.message.error, error.code);
}
if (APIErrors.hasStringField(error, 'info')) {
return new APIError(error.info);
}
if (APIErrors.isGeneralError(error)) {
if (error.code) {
if (typeof error.message === 'string') {
return new APIError(error.message, error.code);
}
if (error.message.error) {
return new APIError(error.message.error.message, error.code);
}
}
if (typeof error.message === 'string') {
return new APIError(error.message);
}
if (APIErrors.hasStringField(error.message, 'code')) {
return new APIError(defaultMessage, error.message.code);
}
}
if (APIErrors.isInternalError(error)) {
const e = error.internal.error;
return new APIError(e.message, undefined, e.description);
}
if (APIErrors.hasStringField(error, 'custom')) {
return new APIError(error.custom);
}
if (
APIErrors.hasStringField(error, 'code') &&
APIErrors.hasStringField(error, 'error') &&
APIErrors.hasStringField(error, 'path')
) {
return new APIError(error.error, error.code);
}
return new APIError(JSON.stringify(error ?? defaultMessage));
}
}