mirror of
https://github.com/versia-pub/api.git
synced 2025-12-06 08:28:19 +01:00
feat: ✨ Add error response to ResponseError
This commit is contained in:
parent
747cc0e63c
commit
a7b7115a88
|
|
@ -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(
|
||||
|
|
@ -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
|
||||
}`,
|
||||
|
|
|
|||
Loading…
Reference in a new issue