-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy patherrors.ts
More file actions
245 lines (222 loc) · 6.41 KB
/
errors.ts
File metadata and controls
245 lines (222 loc) · 6.41 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { AxiosError, AxiosHeaderValue, Method } from "axios";
import {
ErrorCode,
InternalErrorCode,
NotFoundErrorCode,
} from "./apiModel";
/**
*
* @export
* @class FgaError
* @extends {Error}
*/
export class FgaError extends Error {
name = "FgaError";
constructor(err?: Error | string | unknown, msg?: string) {
super(
msg ?? (typeof err === "string"
? err
: `FGA Error${(err as Error)?.message ? `: ${(err as Error).message}` : ""}`
)
);
if ((err as Error)?.stack) {
this.stack = (err as Error).stack;
}
}
}
function getRequestMetadataFromPath(path?: string): {
storeId: string;
endpointCategory: string;
} {
// This function works because all paths start with /stores/{storeId}/{type}
let splitPath: string[] = (path || "").split("/");
if (splitPath.length < 4) {
splitPath = [];
}
const storeId = splitPath[2] || "";
const endpointCategory = splitPath[3] || "";
return { storeId, endpointCategory };
}
const cFGARequestId = "fga-request-id";
function getResponseHeaders(err: AxiosError): any {
return err.response
? Object.fromEntries(
Object.entries(err.response.headers).map(([k, v]) => [
k.toLowerCase(), v,
])
)
: {};
}
/**
*
* @export
* @class FgaApiError
* @extends { FgaError }
*/
export class FgaApiError extends FgaError {
name = "FgaApiError";
public statusCode?: number;
public statusText?: string;
public method?: Method;
public requestURL?: string;
public storeId?: string;
public endpointCategory?: string;
public apiErrorMessage?: string;
public requestData?: any;
public responseData?: any;
public responseHeader?: Record<string, AxiosHeaderValue | undefined>;
public requestId?: string;
constructor(err: AxiosError, msg?: string) {
super(msg ? msg : err);
this.statusCode = err.response?.status;
this.statusText = err.response?.statusText;
this.requestData = err.config?.data;
this.requestURL = err.config?.url;
this.method = err.config?.method as Method;
const { storeId, endpointCategory } = getRequestMetadataFromPath(
err.request?.path
);
this.storeId = storeId;
this.endpointCategory = endpointCategory;
this.apiErrorMessage = (err.response?.data as any)?.message;
this.responseData = err.response?.data;
this.responseHeader = err.response?.headers;
const errResponseHeaders = getResponseHeaders(err);
this.requestId = errResponseHeaders[cFGARequestId];
if ((err as Error)?.stack) {
this.stack = (err as Error).stack;
}
}
}
/**
*
* @export
* @class FgaApiValidationError
* @extends { FgaApiError }
*/
export class FgaApiValidationError extends FgaApiError {
name = "FgaApiValidationError";
public apiErrorCode: ErrorCode;
constructor(err: AxiosError, msg?: string) {
// If there is a better error message, use it instead of the default error
super(err);
this.apiErrorCode = (err.response?.data as any)?.code;
const { endpointCategory } = getRequestMetadataFromPath(err.request?.path);
this.message = msg
? msg
: (err.response?.data as any)?.message
? `FGA API Validation Error: ${err.config?.method} ${endpointCategory} : Error ${(err.response?.data as any)?.message}`
: (err as Error).message;
}
}
/**
*
* @export
* @class FgaApiNotFoundError
* @extends { FgaApiError }
*/
export class FgaApiNotFoundError extends FgaApiError {
name = "FgaApiNotFoundError";
public apiErrorCode: NotFoundErrorCode;
constructor(err: AxiosError, msg?: string) {
// If there is a better error message, use it instead of the default error
super(err);
this.apiErrorCode = (err.response?.data as any)?.code;
this.message = msg
? msg
: (err.response?.data as any)?.message
? `FGA API NotFound Error: ${err.config?.method} : Error ${(err.response?.data as any)?.message}`
: (err as Error).message;
}
}
const cXRateLimit = "x-ratelimit-limit";
const cXRateLimitReset = "x-ratelimit-reset";
/**
*
* @export
* @class FgaApiRateLimitExceededError
* @extends { FgaApiError }
*/
export class FgaApiRateLimitExceededError extends FgaApiError {
name = "FgaApiRateLimitExceededError";
public apiErrorCode?: string;
constructor(err: AxiosError, msg?: string) {
super(err);
this.apiErrorCode = (err.response?.data as any)?.code;
this.message = msg
? msg
: `FGA API Rate Limit Error for ${this.method} ${this.endpointCategory}`;
}
}
/**
*
* @export
* @class FgaApiInternalError
* @extends { FgaApiError }
*/
export class FgaApiInternalError extends FgaApiError {
name = "FgaApiInternalError";
public apiErrorCode: InternalErrorCode;
constructor(err: AxiosError, msg?: string) {
// If there is a better error message, use it instead of the default error
super(err);
const { endpointCategory } = getRequestMetadataFromPath(err.request?.path);
this.apiErrorCode = (err.response?.data as any)?.code;
this.message = msg
? msg
: (err.response?.data as any)?.message
? `FGA API Internal Error: ${err.config?.method} ${endpointCategory} : Error ${(err.response?.data as any)?.message}`
: (err as Error).message;
}
}
/**
*
* @export
* @class FgaApiAuthenticationError
* @extends { FgaApiError }
*/
export class FgaApiAuthenticationError extends FgaApiError {
name = "FgaApiAuthenticationError";
public clientId?: string;
public audience?: string;
public grantType?: string;
public apiErrorCode?: string;
constructor(err: AxiosError) {
super(err);
this.message = `FGA Authentication Error.${err.response?.statusText ? ` ${err.response.statusText}` : ""}`;
this.apiErrorCode = (err.response?.data as any)?.code;
let data: any;
try {
data = JSON.parse(err.config?.data || "{}");
} catch (err) {
/* do nothing */
}
this.clientId = data?.client_id;
this.audience = data?.audience;
this.grantType = data?.grant_type;
}
}
/**
*
* @export
* @class FgaValidationError
* @extends { FgaError }
*/
export class FgaValidationError extends FgaError {
name = "FgaValidationError";
constructor(public field: string, msg?: string) {
super(msg);
}
}
/**
*
* @export
* @class FgaRequiredParamError
* @extends { FgaValidationError }
*/
export class FgaRequiredParamError extends FgaValidationError {
name = "FgaRequiredParamError";
constructor(public functionName: string, field: string, msg?: string) {
super(field, msg);
}
}