Skip to content

Commit 5011954

Browse files
committed
fix: json parse in try:
1 parent 349d7a4 commit 5011954

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/content/adobe-commerce.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ async function fetchProduct(sku, config) {
4848
throw errorWithResponse(resp.status, 'failed to fetch product');
4949
}
5050

51-
const json = await resp.json();
5251
try {
53-
const [productData] = json.data?.products ?? [];
52+
const json = await resp.json();
53+
const [productData] = json?.data?.products ?? [];
5454
if (!productData) {
5555
throw errorWithResponse(404, 'could not find product', json.errors);
5656
}
@@ -93,9 +93,9 @@ async function fetchVariants(sku, config) {
9393
throw errorWithResponse(resp.status, 'failed to fetch variants');
9494
}
9595

96-
const json = await resp.json();
9796
try {
98-
const { variants } = json.data.variants;
97+
const json = await resp.json();
98+
const { variants } = json?.data?.variants ?? {};
9999
return variantsAdapter(variants);
100100
} catch (e) {
101101
console.error('failed to parse variants: ', e);
@@ -134,10 +134,10 @@ async function lookupProductSKU(urlkey, config) {
134134
throw errorWithResponse(resp.status, 'failed to fetch product sku');
135135
}
136136

137-
const json = await resp.json();
138137
try {
139-
const [product] = json.data?.products?.items ?? [];
140-
if (!product) {
138+
const json = await resp.json();
139+
const [product] = json?.data?.products?.items ?? [];
140+
if (!product?.sku) {
141141
throw errorWithResponse(404, 'could not find product sku', json.errors);
142142
}
143143
return product.sku;

0 commit comments

Comments
 (0)