Shopify / Shopify/shopify_python_api

Returned error in result missing error subject (discovered in metafield create/update)

Open
#204 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request
Dominant language
Python
Stars
1.4k
Forks
388
Avg merge
5h 39m
Merged PRs (30d)
1

Description

The shopify API returns the subject of an error as a key in the json error string, and pyactiveresource sometimes adds the value but not the key when it unpacks the string.

This probably shows up for some errors generated when adding or modifying variants or metafields, as they show up as lists of hashes in the object attributes. I hit a problem when adding or updating a metafield on a variant, and decided to dig.

The issue is in class Errors in activeresource.py:

    def from_hash(self, messages):
        attribute_keys = self.base.attributes.keys()
        for key, errors in six.iteritems(messages):
            for message in errors:
                if key in attribute_keys:
                    self.add(key, message)
                else:
                    self.add_to_base(message)

An error return like this:

{
    "errors": {
        "metafields.namespace": ["can't be blank", "is too short (minimum is 3 characters)"]
    }
}

(caused by trying to create or update a metafield with the key "name-space" instead of "namespace" :) will lose the important tidbit, the field causing the issue: "metafields" is a key in the variant attributes, but "metafields.namespace" is not.

Perhaps this might be a more robust approach?

    def from_hash(self, messages):
        attribute_keys = self.base.attributes.keys()
        for key, errors in six.iteritems(messages):
            for message in errors:
                if key.split('.')[0] in attribute_keys:
                    self.add(key, message)
                else:
                    self.add_to_base(' '.join([key, message]))

Thanks!

Rick

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in activeresource.py at class Errors and its from_hash method, then inspect how nested error keys are unpacked. Reproduce the metafield-on-variant case using the shown metafields.namespace response. Done means the returned error preserves the subject key instead of losing the field causing the error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.