Skip to content

Commit

Permalink
Merge branch 'master' into feat/new-eslint-config
Browse files Browse the repository at this point in the history
  • Loading branch information
0t4u authored Aug 10, 2024
2 parents 8a6e48c + ca8d6b9 commit 1bf21a0
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions src/node/Rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ export class Rest {
* Make a request to Lavalink
* @param fetchOptions.endpoint Lavalink endpoint
* @param fetchOptions.options Options passed to fetch
* @throws `RestError` when encountering a Lavalink error response
* @internal
*/
protected async fetch<T = unknown>(fetchOptions: FetchOptions) {
Expand Down Expand Up @@ -391,13 +392,14 @@ export class Rest {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const response = await request
.json()
.catch(() => null);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (!response?.message)
throw new Error(`Rest request failed with response code: ${request.status}`);
else
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
throw new Error(`Rest request failed with response code: ${request.status} | message: ${response.message}`);
.catch(() => null) as LavalinkRestError | null;
throw new RestError(response ?? {
timestamp: Date.now(),
status: request.status,
error: 'Unknown Error',
message: 'Unexpected error response from Lavalink server',
path: endpoint,
});
}
try {
return await request.json() as T;
Expand All @@ -406,3 +408,32 @@ export class Rest {
}
}
}

interface LavalinkRestError {
timestamp: number;
status: number;
error: string;
trace?: string;
message: string;
path: string;
}

export class RestError extends Error {
public timestamp: number;
public status: number;
public error: string;
public trace?: string;
public path: string;

constructor({ timestamp, status, error, trace, message, path }: LavalinkRestError) {
super(`Rest request failed with response code: ${status}${message ? ` | message: ${message}` : ''}`);
this.name = 'RestError';
this.timestamp = timestamp;
this.status = status;
this.error = error;
this.trace = trace;
this.message = message;
this.path = path;
Object.setPrototypeOf(this, new.target.prototype);
}
}

0 comments on commit 1bf21a0

Please sign in to comment.