Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding serde_json::Error::code() functionnality #865

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

d34db4b3
Copy link

@d34db4b3 d34db4b3 commented Mar 3, 2022

Hi,
With this commit, I'm trying to add the ability to identify the precise error that occurred during de/serialization. There should be no breaking change and a new method serde_json::Error::Code() now exposes the internal code of the error.

The original purpose is to provide translated information on the error (for API body request validation for example) as well as handling errors more precisely.

It is not yet perfect as serde injects some data (eg. https://github.com/serde-rs/serde) and changing this behavior would probably be breaking.

A simple example of the new functionality would be :

match err.code() {
    ErrorCode::InvalidType(unexp, exp) => {
        println!("Type invalide: `{}` au lieu de `{}`", unexp, exp);
    }
    ErrorCode::InvalidValue(unexp, exp) => {
        println!("Valeur invalide: `{}` au lieu de `{}`", unexp, exp);
    }
    ErrorCode::InvalidLength(len, exp) => {
        println!("Taille invalide: `{}` au lieu de `{}`", len, exp);
    }
    e => {
        println!("Autre erreur: {}", e);
    }
}

@d34db4b3
Copy link
Author

d34db4b3 commented Mar 4, 2022

I've taken the errors into account and will release a fix soon.

Copy link
Author

@d34db4b3 d34db4b3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be fixed now.
EDIT: nevermind there are still a bug with older version of rust

Copy link
Author

@d34db4b3 d34db4b3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed now 👍

@d34db4b3
Copy link
Author

Any update on the feature ?

@doylemark
Copy link

Could this PR be considered for review? It seems quite important to be able to extract some information about the specific error that occurs during deserialization.

To give an example, for deserialising POST request bodies, when deserialization fails it would be nice to be able to provide meaningful errors in a 400 response explaining why the body is not formed correctly.

Doing validation (regex/min lengths etc.) after a successful deserialization is possible, but when deserialization fails it's quite difficult to build an appropriate error message from just the .to_string() output and line/col number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants