seyfert 2.1.1-dev-11674326808.0 → 2.1.1-dev-11679252017.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/api/api.d.ts +2 -1
- package/lib/api/api.js +23 -9
- package/package.json +1 -1
package/lib/api/api.d.ts
CHANGED
|
@@ -22,7 +22,8 @@ export declare class ApiHandler {
|
|
|
22
22
|
get proxy(): APIRoutes;
|
|
23
23
|
globalUnblock(): void;
|
|
24
24
|
request<T = unknown>(method: HttpMethods, url: `/${string}`, { auth, ...request }?: ApiRequestOptions): Promise<T>;
|
|
25
|
-
parseError(route: `/${string}`, response: Response, result:
|
|
25
|
+
parseError(method: HttpMethods, route: `/${string}`, response: Response, result: string | Record<string, any>): Error;
|
|
26
|
+
parseValidationError(data: Record<string, any>, path?: string, errors?: string[]): string[];
|
|
26
27
|
handle50X(method: HttpMethods, url: `/${string}`, request: ApiRequestOptions, next: () => void): Promise<unknown>;
|
|
27
28
|
handle429(route: string, method: HttpMethods, url: `/${string}`, request: ApiRequestOptions, response: Response, result: string, next: () => void, reject: (err: unknown) => void, now: number): Promise<unknown>;
|
|
28
29
|
clearResetInterval(route: string): void;
|
package/lib/api/api.js
CHANGED
|
@@ -132,7 +132,7 @@ class ApiHandler {
|
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
134
|
}
|
|
135
|
-
const parsedError = this.parseError(route, response, result);
|
|
135
|
+
const parsedError = this.parseError(method, route, response, result);
|
|
136
136
|
this.debugger?.warn(parsedError);
|
|
137
137
|
reject(parsedError);
|
|
138
138
|
return;
|
|
@@ -170,19 +170,33 @@ class ApiHandler {
|
|
|
170
170
|
}
|
|
171
171
|
});
|
|
172
172
|
}
|
|
173
|
-
parseError(route, response, result) {
|
|
173
|
+
parseError(method, route, response, result) {
|
|
174
174
|
let errMessage = '';
|
|
175
|
-
if (typeof result === 'object'
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
errMessage += `${JSON.stringify(result.errors, null, 2)}\n`;
|
|
175
|
+
if (typeof result === 'object') {
|
|
176
|
+
errMessage += `${result.message ?? 'Unknown'} ${result.code ?? ''}\n[${response.status} ${response.statusText}] ${method} ${route}`;
|
|
177
|
+
if ('errors' in result) {
|
|
178
|
+
const errors = this.parseValidationError(result.errors);
|
|
179
|
+
errMessage += `\n${errors.join('\n') || JSON.stringify(result.errors, null, 2)}`;
|
|
181
180
|
}
|
|
182
181
|
}
|
|
183
|
-
|
|
182
|
+
else {
|
|
183
|
+
errMessage = `[${response.status} ${response.statusText}] ${method} ${route}`;
|
|
184
|
+
}
|
|
184
185
|
return new Error(errMessage);
|
|
185
186
|
}
|
|
187
|
+
parseValidationError(data, path = '', errors = []) {
|
|
188
|
+
for (const key in data) {
|
|
189
|
+
if (key === '_errors') {
|
|
190
|
+
for (const error of data[key]) {
|
|
191
|
+
errors.push(`${path.slice(0, -1)} [${error.code}]: ${error.message}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
else if (typeof data[key] === 'object') {
|
|
195
|
+
this.parseValidationError(data[key], `${path}${key}.`, errors);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return errors;
|
|
199
|
+
}
|
|
186
200
|
async handle50X(method, url, request, next) {
|
|
187
201
|
const wait = Math.floor(Math.random() * 1900 + 100);
|
|
188
202
|
this.debugger?.warn(`Handling a 50X status, retrying in ${wait}ms`);
|