Skip to content

Commit cd6d218

Browse files
committed
fix: throw if no price range on product or variant
1 parent b3b4c48 commit cd6d218

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

src/content/adobe-commerce.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ async function fetchProduct(sku, config) {
5757
const product = productAdapter(config, productData);
5858
return product;
5959
} catch (e) {
60-
console.error('failed to parse product: ', e);
60+
if (e.response) {
61+
throw errorWithResponse(e.response.status, e.message);
62+
}
6163
throw errorWithResponse(500, 'failed to parse product response');
6264
}
6365
}
@@ -98,7 +100,9 @@ async function fetchVariants(sku, config) {
98100
const { variants } = json?.data?.variants ?? {};
99101
return variantsAdapter(config, variants);
100102
} catch (e) {
101-
console.error('failed to parse variants: ', e);
103+
if (e.response) {
104+
throw errorWithResponse(e.response.status, e.message);
105+
}
102106
throw errorWithResponse(500, 'failed to parse variants response');
103107
}
104108
}
@@ -143,6 +147,9 @@ async function lookupProductSKU(urlkey, config) {
143147
return product.sku;
144148
} catch (e) {
145149
console.error('failed to parse product sku: ', e);
150+
if (e.response) {
151+
throw errorWithResponse(e.response.status, e.message);
152+
}
146153
throw errorWithResponse(500, 'failed to parse product sku response');
147154
}
148155
}

src/content/queries/cs-product.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import { forceImagesHTTPS } from '../../utils/http.js';
13+
import { errorWithResponse, forceImagesHTTPS } from '../../utils/http.js';
1414
import { gql, parseRating, parseSpecialToDate } from '../../utils/product.js';
1515

1616
/**
@@ -91,6 +91,10 @@ export const adapter = (config, productData) => {
9191
} : null,
9292
};
9393

94+
if (!minPrice && !maxPrice) {
95+
throw errorWithResponse(400, 'no product price range found');
96+
}
97+
9498
if (config.attributeOverrides?.product) {
9599
Object.entries(config.attributeOverrides.product).forEach(([key, value]) => {
96100
product.attributeMap[key] = product.attributeMap[value] ?? product[key];

src/content/queries/cs-variants.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import { forceImagesHTTPS } from '../../utils/http.js';
13+
import { errorWithResponse, forceImagesHTTPS } from '../../utils/http.js';
1414
import { gql, parseRating, parseSpecialToDate } from '../../utils/product.js';
1515

1616
/**
@@ -37,22 +37,26 @@ export const adapter = (config, variants) => variants.map(({ selections, product
3737
prices: {
3838
regular: {
3939
// TODO: determine whether to use min or max
40-
amount: minPrice.regular.amount.value,
41-
currency: minPrice.regular.amount.currency,
42-
maximumAmount: maxPrice.regular.amount.value,
43-
minimumAmount: minPrice.regular.amount.value,
40+
amount: minPrice?.regular.amount.value,
41+
currency: minPrice?.regular.amount.currency,
42+
maximumAmount: maxPrice?.regular.amount.value,
43+
minimumAmount: minPrice?.regular.amount.value,
4444
},
4545
final: {
4646
// TODO: determine whether to use min or max
47-
amount: minPrice.final.amount.value,
48-
currency: minPrice.final.amount.currency,
49-
maximumAmount: maxPrice.final.amount.value,
50-
minimumAmount: minPrice.final.amount.value,
47+
amount: minPrice?.final.amount.value,
48+
currency: minPrice?.final.amount.currency,
49+
maximumAmount: maxPrice?.final.amount.value,
50+
minimumAmount: minPrice?.final.amount.value,
5151
},
5252
},
5353
selections: (selections ?? []).sort(),
5454
};
5555

56+
if (!minPrice && !maxPrice) {
57+
throw errorWithResponse(400, 'no variant price range found');
58+
}
59+
5660
if (config.attributeOverrides?.variant) {
5761
Object.entries(config.attributeOverrides.variant).forEach(([key, value]) => {
5862
variant.attributeMap[key] = variant.attributeMap[value] ?? variant[key];

0 commit comments

Comments
 (0)