diff --git a/.changeset/warm-bobcats-prove.md b/.changeset/warm-bobcats-prove.md new file mode 100644 index 0000000000..b79bbf85d0 --- /dev/null +++ b/.changeset/warm-bobcats-prove.md @@ -0,0 +1,6 @@ +--- +"@medusajs/medusa": patch +"integration-tests-http": patch +--- + +fix(medusa): cart now returns 404 when not found diff --git a/integration-tests/http/__tests__/cart/store/cart.spec.ts b/integration-tests/http/__tests__/cart/store/cart.spec.ts index fe8f731a76..d690546102 100644 --- a/integration-tests/http/__tests__/cart/store/cart.spec.ts +++ b/integration-tests/http/__tests__/cart/store/cart.spec.ts @@ -9,11 +9,7 @@ import { PromotionStatus, PromotionType, } from "@medusajs/utils" -import { - createAdminUser, - generatePublishableKey, - generateStoreHeaders, -} from "../../../../helpers/create-admin-user" +import { createAdminUser, generatePublishableKey, generateStoreHeaders, } from "../../../../helpers/create-admin-user" import { setupTaxStructure } from "../../../../modules/__tests__/fixtures" import { createAuthenticatedCustomer } from "../../../../modules/helpers/create-authenticated-customer" import { medusaTshirtProduct } from "../../../__fixtures__/product" @@ -152,6 +148,17 @@ medusaIntegrationTestRunner({ ).data.promotion }) + describe("GET /store/carts/[id]", () => { + it("should return 404 when trying to fetch a cart that does not exist", async () => { + const response = await api.get( + `/store/carts/fake`, + storeHeadersWithCustomer + ).catch((e) => e) + + expect(response.response.status).toEqual(404) + }) + }) + describe("POST /store/carts", () => { it("should successfully create a cart", async () => { const response = await api.post( diff --git a/packages/medusa/src/api/store/carts/helpers.ts b/packages/medusa/src/api/store/carts/helpers.ts index 6a237d2680..25311acc6c 100644 --- a/packages/medusa/src/api/store/carts/helpers.ts +++ b/packages/medusa/src/api/store/carts/helpers.ts @@ -1,6 +1,7 @@ import { MedusaContainer } from "@medusajs/framework/types" import { ContainerRegistrationKeys, + MedusaError, remoteQueryObjectFromString, } from "@medusajs/framework/utils" @@ -18,5 +19,12 @@ export const refetchCart = async ( const [cart] = await remoteQuery(queryObject) + if (!cart) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `Cart with id '${id}' not found` + ) + } + return cart }