Improve error handling
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import type { Printful } from "./util/types";
|
||||
import { FetchError, type DeepPartial } from "./util/misc";
|
||||
import { type DeepPartial } from "./util/misc";
|
||||
|
||||
const parsePayload = (res: Response): Promise<unknown> =>
|
||||
res.json().catch(() => res.text().catch(() => undefined));
|
||||
|
||||
type ClientOptions = {
|
||||
token: string;
|
||||
@@ -11,6 +14,17 @@ type Credentials = {
|
||||
authHeaders: Record<string, string>;
|
||||
};
|
||||
|
||||
export class PrintfulError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public status: number,
|
||||
public payload: unknown,
|
||||
) {
|
||||
super(message);
|
||||
this.name = "PrintfulError";
|
||||
}
|
||||
}
|
||||
|
||||
class VariantsClient {
|
||||
constructor(private creds: Credentials) { }
|
||||
|
||||
@@ -20,7 +34,7 @@ class VariantsClient {
|
||||
headers: this.creds.authHeaders,
|
||||
});
|
||||
if (res.status === 404 || res.status === 400) return;
|
||||
if (!res.ok) throw new FetchError("Failed to get Printful variant", res);
|
||||
if (!res.ok) throw new PrintfulError("Failed to get Printful variant", res.status, await parsePayload(res));
|
||||
const payload = (await res.json()) as Printful.Products.MetaDataSingle<Printful.Products.SyncVariant>;
|
||||
return payload.result;
|
||||
}
|
||||
@@ -40,7 +54,7 @@ class ProductsClient {
|
||||
method: "GET",
|
||||
headers: this.creds.authHeaders,
|
||||
});
|
||||
if (!res.ok) throw new FetchError("Failed to get all Printful products", res);
|
||||
if (!res.ok) throw new PrintfulError("Failed to get all Printful products", res.status, await parsePayload(res));
|
||||
const payload = (await res.json()) as Printful.Products.MetaDataMulti<Printful.Products.SyncProduct>;
|
||||
allSyncProducts.push(...payload.result);
|
||||
offset = allSyncProducts.length;
|
||||
@@ -55,7 +69,7 @@ class ProductsClient {
|
||||
headers: this.creds.authHeaders,
|
||||
});
|
||||
if (res.status === 404 || res.status === 400) return;
|
||||
if (!res.ok) throw new FetchError("Failed to get Printful product", res);
|
||||
if (!res.ok) throw new PrintfulError("Failed to get Printful product", res.status, await parsePayload(res));
|
||||
const payload = (await res.json()) as Printful.Products.MetaDataSingle<Printful.Products.Product>;
|
||||
return payload.result;
|
||||
}
|
||||
@@ -69,7 +83,7 @@ class ProductsClient {
|
||||
},
|
||||
body: JSON.stringify(printfulProduct),
|
||||
});
|
||||
if (!res.ok) throw new FetchError("Failed to update Printful product", res);
|
||||
if (!res.ok) throw new PrintfulError("Failed to update Printful product", res.status, await parsePayload(res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +99,7 @@ class OrdersClient {
|
||||
},
|
||||
body: JSON.stringify(printfulOrder),
|
||||
});
|
||||
if (!res.ok) throw new FetchError("Failed to create printful order", res);
|
||||
if (!res.ok) throw new PrintfulError("Failed to create Printful order", res.status, await parsePayload(res));
|
||||
}
|
||||
|
||||
async getAll(offset = 0): Promise<Printful.Orders.Order[]> {
|
||||
@@ -95,7 +109,7 @@ class OrdersClient {
|
||||
method: "GET",
|
||||
headers: this.creds.authHeaders,
|
||||
});
|
||||
if (!res.ok) throw new FetchError("Failed to get all Printful orders", res);
|
||||
if (!res.ok) throw new PrintfulError("Failed to get all Printful orders", res.status, await parsePayload(res));
|
||||
const payload = (await res.json()) as Printful.Products.MetaDataMulti<Printful.Orders.Order>;
|
||||
allPrintfulOrders.push(...payload.result);
|
||||
offset = allPrintfulOrders.length;
|
||||
|
||||
Reference in New Issue
Block a user