question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Freature request: allow handle_error to suppress an error.

See original GitHub issue

In do_load you have the following code block:

        if errors:
            exc = ValidationError(errors, data=data, valid_data=result)
            self.handle_error(exc, data, many=many, partial=partial)
            raise exc

So the exception is raised unconditionally. This means that there is no way for a custom schema to correct or suppress an error raised by a sub-schema.

Our use case is wanting to have a schema in which some fields are validated, but the remainder are passed “as is” (they are massaged and validated later on in our data pipeline). In Marshmallow 2 we set our schema to strict=False and had our handle_error raise on the fields we wanted validated. As far as I can see there is no equivalent functionality in Marshmallow 3. This could be solved by having the code that calls handle_error allow it to signal that the exception be ignored. For example, it could treat a return value of True from handle_error as signalling that the error has been successfully handled and should not be raised.

(By the way, this use case is similar to that mentioned in #1198, but we aren’t serializing an object, just a json data structure. We are currently using a @post_dump method to include the unknown fields in the dump, but I haven’t actually gotten far enough yet to know if that will actually work in Marshmallow 3 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
deckar01commented, Oct 11, 2019

The validation error that is raised has a valid_data property you can use to recover the valid data.

https://marshmallow.readthedocs.io/en/stable/quickstart.html#validation

I think this is what @bitdancer ended up doing:

from marshmallow import Schema, fields, ValidationError

class Test(Schema):
    foo = fields.String()
    bar = fields.String()
    
    def load(self, *args, **kwargs):
        try:
            return super().load(*args, **kwargs)
        except ValidationError as e:
            return e.valid_data

Test().load({'foo': 123, 'bar': 'ok'})
# {'bar': 'ok'}
1reaction
bitdancercommented, Sep 23, 2019

Ah, nevermind. What I requested wouldn’t do what we wanted because _do_load is called from validate as well.

What I ended up with was adding a ‘load’ method to our schema that calls super inside a try/except and does what we want. Sorry for wasting your time 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling operation errors - Apollo GraphQL Docs
Apollo Client helps you handle these errors according to their type, enabling you to show appropriate information to the user when an error...
Read more >
A Definitive Guide to Handling Errors in JavaScript - Kinsta
Getting tripped up by errors in your JavaScript? We'll show you how to tame those errors so you can get back to developing...
Read more >
Error Handling with Angular 8 - Tips and Best Practices - Rollbar
One traditional way of handling errors in Angular is to provide an ErrorHandler class. This class can be extended to create your own...
Read more >
Error handling in Combine explained with code examples
Error handling in Swift Combine explained by covering operators like mapError, catch, replaceError, assertOnFailure, and how to map to the ...
Read more >
Advanced Features: Error Handling - Next.js
Handle errors in your Next.js app. ... the page from crashing, it allows you to provide a custom fallback component and even log...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found