seyfert 2.1.1-dev-11674440750.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 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(method: HttpMethods, route: `/${string}`, response: Response, result: unknown): Error;
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
@@ -172,17 +172,31 @@ class ApiHandler {
172
172
  }
173
173
  parseError(method, route, response, result) {
174
174
  let errMessage = '';
175
- if (typeof result === 'object' && result) {
176
- if ('message' in result) {
177
- errMessage += `${result.message}${'code' in result ? ` ${result.code}` : ''}\n`;
178
- }
179
- if ('errors' in result && result) {
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
- errMessage += ` at [${response.status} ${response.statusText}] ${method} ${route}\n`;
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`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "seyfert",
3
- "version": "2.1.1-dev-11674440750.0",
3
+ "version": "2.1.1-dev-11679252017.0",
4
4
  "description": "The most advanced framework for discord bots",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",