quarkusio/quarkus · error · IllegalStateException

There is already an active cart

Error message

There is already an active cart

What it means

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.

Source

Thrown at integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/CartResource.java:60

    @GET
    @Path("/{id}")
    public Cart findById(@PathParam("id") Long id) {
        return this.cartRepository.findById(id).orElse(null);
    }

    @POST
    @Path("/customer/{id}")
    public Cart create(@PathParam("id") Long customerId) {
        if (this.getActiveCartForCustomer(customerId) == null) {
            Customer customer = this.customerRepository.findById(customerId)
                    .orElseThrow(() -> new IllegalStateException("The Customer does not exist!"));

            Cart cart = new Cart(customer, CartStatus.NEW);

            return this.cartRepository.save(cart);
        } else {
            throw new IllegalStateException("There is already an active cart");
        }
    }

    @DELETE
    @Path("/{id}")
    public void delete(@PathParam("id") Long id) {
        Cart cart = this.cartRepository.findById(id)
                .orElseThrow(() -> new IllegalStateException("Cannot find Cart with id " + id));

        cart.setStatus(CANCELED);
        this.cartRepository.save(cart);
    }
}

View on GitHub (pinned to e1c734241f)

Solutions

  1. Close or delete the existing active cart for the customer before creating a new one
  2. Catch IllegalStateException in the resource and return HTTP 409 Conflict with a clear message
  3. Make the create idempotent: return the existing active cart instead of throwing

Example fix

// before
} else {
    throw new IllegalStateException("There is already an active cart");
}
// after
} else {
    return Response.status(Response.Status.CONFLICT)
        .entity("Customer already has an active cart").build();
}
Defensive patterns

Strategy: try-catch

Validate before calling

List<Cart> active = cartRepository.findByCustomerIdAndStatus(customerId, CartStatus.NEW);
if (!active.isEmpty()) {
    // reuse active.get(0) or close it before creating a new cart
}

Try / catch

try {
    cartResource.create(customerId);
} catch (IllegalStateException e) {
    if (e.getMessage().contains("already an active cart")) {
        // fetch and reuse or close the existing cart
    }
}

Prevention

When it happens

Trigger: POSTing to the cart creation endpoint twice for the same customer while the first cart is still in CartStatus.NEW (not CLOSED/PAID).

Common situations: 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.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/2c0048b09a4f4a62. Report an issue: GitHub.