Skip to content

Commit

Permalink
feat: ✨ Add error response to ResponseError
Browse files Browse the repository at this point in the history
  • Loading branch information
CPlusPatch committed Jun 16, 2024
1 parent 747cc0e commit a7b7115
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions client/lysand/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ const objectToFormData = (obj: ConvertibleObject): FormData => {
*
* Throws if the request returns invalid or unexpected data.
*/
export class ResponseError extends Error {}
export class ResponseError<ReturnType> extends Error {
constructor(
public response: Output<ReturnType>,
message: string,
) {
super(message);
this.name = "ResponseError";
}
}

export class BaseClient {
constructor(
Expand All @@ -86,7 +94,13 @@ export class BaseClient {

if (!result.ok) {
const error = isJson ? await result.json() : await result.text();
throw new ResponseError(
throw new ResponseError<{
error?: string;
}>(
{
data: error,
headers: result.headers,
},
`Request failed (${result.status}): ${
error.error || error.message || result.statusText
}`,
Expand Down

0 comments on commit a7b7115

Please sign in to comment.