Skip to content

Conversation

@touilleMan
Copy link
Member

Hi there,

Working with save_condition in save, I realised using an OperationError exception is too vague when trying to return fine errors to client, example:

class Doc(Document):
    version = fields.IntField(default=1)
    field = fields.StringField(unique=True)

doc_1 = Doc(field='unique').save()
try:
    doc_2 = Doc(field='unique').save() # raises NotUniqueError, subclass of OperationError
except OperationError:
    # Should handle HTTP 400 error
    pass
doc_1.field = 'two'
http_if_match = 42 # Client provide If-Match header in HTTP request
try:
    doc_1.save(save_condition={'version': http_if_match}) # raises OperationError
except OperationError:
    # Should handle HTTP 412
    pass

I solve this by replacing the OperationError triggered by an invalid save_condition by a new SaveConditionError (subclass of OperationError, to mimic the behaviour of NotUniqueError)

Review on Reviewable

@touilleMan touilleMan added this to the 0.10.1 milestone Jul 19, 2015
touilleMan added a commit that referenced this pull request Jul 19, 2015
Use SaveConditionError instead of OperationError in save_condition
@touilleMan touilleMan merged commit f8890ca into MongoEngine:master Jul 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants