{"record":{"id":"2c0048b09a4f4a62","repo":"quarkusio/quarkus","slug":"there-is-already-an-active-cart","errorCode":null,"errorMessage":"There is already an active cart","messagePattern":"There is already an active cart","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/CartResource.java","lineNumber":60,"sourceCode":"\n    @GET\n    @Path(\"/{id}\")\n    public Cart findById(@PathParam(\"id\") Long id) {\n        return this.cartRepository.findById(id).orElse(null);\n    }\n\n    @POST\n    @Path(\"/customer/{id}\")\n    public Cart create(@PathParam(\"id\") Long customerId) {\n        if (this.getActiveCartForCustomer(customerId) == null) {\n            Customer customer = this.customerRepository.findById(customerId)\n                    .orElseThrow(() -> new IllegalStateException(\"The Customer does not exist!\"));\n\n            Cart cart = new Cart(customer, CartStatus.NEW);\n\n            return this.cartRepository.save(cart);\n        } else {\n            throw new IllegalStateException(\"There is already an active cart\");\n        }\n    }\n\n    @DELETE\n    @Path(\"/{id}\")\n    public void delete(@PathParam(\"id\") Long id) {\n        Cart cart = this.cartRepository.findById(id)\n                .orElseThrow(() -> new IllegalStateException(\"Cannot find Cart with id \" + id));\n\n        cart.setStatus(CANCELED);\n        this.cartRepository.save(cart);\n    }\n}\n","sourceCodeStart":42,"sourceCodeEnd":74,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/CartResource.java#L42-L74","documentation":"CartResource.create enforces a business rule that a customer may hold only one active cart: it first checks for an existing cart in status NEW; if one exists it throws IllegalStateException('There is already an active cart'), which Jakarta REST maps to HTTP 500 unless an exception mapper handles it.","triggerScenarios":"POSTing to the cart creation endpoint twice for the same customer while the first cart is still in CartStatus.NEW (not CLOSED/PAID).","commonSituations":"E-commerce carts left open from prior test runs or abandoned sessions; tests that don't clean up carts between runs; concurrent creation requests racing past the check.","solutions":["Close or delete the existing active cart for the customer before creating a new one","Catch IllegalStateException in the resource and return HTTP 409 Conflict with a clear message","Make the create idempotent: return the existing active cart instead of throwing"],"exampleFix":"// before\n} else {\n    throw new IllegalStateException(\"There is already an active cart\");\n}\n// after\n} else {\n    return Response.status(Response.Status.CONFLICT)\n        .entity(\"Customer already has an active cart\").build();\n}","handlingStrategy":"try-catch","validationCode":"List<Cart> active = cartRepository.findByCustomerIdAndStatus(customerId, CartStatus.NEW);\nif (!active.isEmpty()) {\n    // reuse active.get(0) or close it before creating a new cart\n}\n","typeGuard":null,"tryCatchPattern":"try {\n    cartResource.create(customerId);\n} catch (IllegalStateException e) {\n    if (e.getMessage().contains(\"already an active cart\")) {\n        // fetch and reuse or close the existing cart\n    }\n}","preventionTips":["Check for an existing NEW cart before create; make create idempotent","Clean up carts in test teardown to avoid cross-run interference","Return 409 Conflict instead of a generic IllegalStateException for client clarity"],"tags":["jpa","spring-data","rest","state-conflict"],"backgroundTag":"duplicate-active-entity","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}