Skip to content

Provide better error messages if slug contains illegal characters #78

@benjaminwil

Description

@benjaminwil

Processors::Product finds or initializes a new product based on the @data["Handle"]. Then, later on, it assigns the slug again to the @data["Handle"]. This is normally not a problem. In a recent import, however, we came across a handle/slug data issue that was difficult to debug. We think this is due to how Solidus generates valid slugs from invalid input.

Reproduction steps

Here's how you can reproduce:

  1. In your product import CSV, add a Handle with an invalid character. i.e. arts-&-crafts-box
  2. Run the product import.
  3. Check the slug of the created product. (It should be arts-crafts-box.)
  4. Run the import again.

Current behaviour

A new product is initialized for arts-&-crafts-box, then the slug is re-generated to be arts-crafts-box, and we end up with a ActiveRecord::InvalidRecord Slug has already been taken error.

Expected behaviour

The arts-crafts-box product is found and loaded. No new product is initialized.

Solution

I am not sure what the best solution would be. Perhaps there is a more nuanced problem in Solidus where Product.find_or_initialize_by(slug: slug) is not the best way to find or initialize a product.

I think at the very least we could validate that the slug input in the CSV would be a valid slug as written. In this example, the first import would complete successfully, but the user would expect their product to be at /product/arts-&-crafts-box and they might be confused and think their import didn't work. The user's second import would fail, and it would be hard for them to figure out why.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions