fix(medusa): throw proper error when product doesn't exist w/ key header (#5745)

**What**
- ensure that an error is raised properly if the requested product doesn't exist and a publishable-key header is set

**Why**
- previously endpoints would hang if the product didn't exist and requests would time out


closes #5724
This commit is contained in:
Philip Korsholm
2023-11-28 10:24:50 +00:00
committed by GitHub
parent 870d686136
commit e4bfa6c88a
4 changed files with 92 additions and 24 deletions
@@ -26,13 +26,18 @@ async function validateProductSalesChannelAssociation(
const { sales_channel_ids: salesChannelIds } =
await publishableKeyService.getResourceScopes(pubKey)
if (
salesChannelIds.length &&
!(await productService.isProductInSalesChannels(
let isProductInSalesChannel = false
try {
isProductInSalesChannel = await productService.isProductInSalesChannels(
req.params.id,
salesChannelIds
))
) {
)
} catch (error) {
next(error)
}
if (salesChannelIds.length && !isProductInSalesChannel) {
req.errors = req.errors ?? []
req.errors.push(
`Product with id: ${req.params.id} is not associated with sales channels defined by the Publishable API Key passed in the header of the request.`
@@ -28,13 +28,19 @@ async function validateProductVariantSalesChannelAssociation(
const { sales_channel_ids: salesChannelIds } =
await publishableKeyService.getResourceScopes(pubKey)
if (
salesChannelIds.length &&
!(await productVariantService.isVariantInSalesChannels(
req.params.id,
salesChannelIds
))
) {
let isVariantInSalesChannel = false
try {
isVariantInSalesChannel =
await productVariantService.isVariantInSalesChannels(
req.params.id,
salesChannelIds
)
} catch (error) {
next(error)
}
if (salesChannelIds.length && !isVariantInSalesChannel) {
req.errors = req.errors ?? []
req.errors.push(
`Variant with id: ${req.params.id} is not associated with sales channels defined by the Publishable API Key passed in the header of the request.`