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.

[RFC] Namespace error structures by location in response

See original GitHub issue

I think there’s a case we never really considered: two schemas in a different location with a common field name.

In webargs 5, this is not possible in a single use_args call. In a multi use_args call, it depends if the user specifies the locations. The response is ambiguous about which field is wrong.

In current implementation in webargs 6, the response is ambiguous. A way to fix this would be to namespace the error structures by location.

Just thinking out loud, writing this here before I forget.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
sirosencommented, Jan 24, 2020

Were you thinking something like these structures, with the location as a top-level namespace for the error messages?

{"body":{"foo":["Not a valid integer."]}}
{"query":{"bar":["Not a valid list."]}}

I’m looking at maybe doing this by rewriting the error messages in Parser._on_validation_error. Does that seem like a good idea? I’m also looking a bit at how we could change the error messages when Schema.load gets called, but I’m not seeing a way.

0reactions
sirosencommented, Feb 14, 2020

I don’t see this issue as important or urgent, but overall, I think it would be a nice fix.

I mostly agree, but I think we’re free to make changes to the messages field now (in 6.0) which we can’t do later without another major release. So I’d like to decide this before v6 is finished.

Well I think it is better this way [using the caller’s location value]. More flexible. I was just mentioning it in case you didn’t really think of it. I thought it was better to discuss it and decide it on purpose. But it is better this way.

👍 Cool. I didn’t really think about it before, so if you’re happy with what’s in #463, so am I.

About WebargsValidationError, it is still unclear to what the difference would be and why choose an option over the other.

Creating a new error class with location in a separate field is what I would do if I wanted to make this change in a minor release. Changing messages is a backwards incompatible change for anyone parsing that field’s contents.

here's a simple example app which parses the messages
import marshmallow as ma
from webargs.flaskparser import FlaskParser

import flask
from werkzeug.exceptions import BadRequest
from werkzeug.serving import run_simple

parser = FlaskParser()


@parser.error_handler
def handle_webargs_error(error, request, schema, status_code, headers):
    messages = []
    for field, field_messages in error.messages.items():
        messages.append(f"{field}: {field_messages[0]}")
    message = ", ".join(messages)
    raise BadRequest(description=f"Request failed input validation. {message}")


app = flask.Flask(__name__)


@app.route("/")
@parser.use_args({"foo": ma.fields.Int()}, locations=("query",))
def fooroute(foo):
    return {"foo": foo}


run_simple("localhost", 5000, app)

If we rewrite the messages field in 6.0, then we’re asking anyone with such an error handler to rewrite it.

I don’t think this kind of rewrite would be hard for anyone unless they’re doing very exotic/complex stuff. And in that case, they might not want to rush to v6.0 anyway.


After writing a concrete example of something parsing messages, I feel less need to try to avoid changing it. Users could ignore the change by just traversing one level of the messages dict, if they so choose, and it’s really not that different to have the information there vs in another attribute of the exception.

Given the original suggestion was for the version in #463, let’s just go with that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

RFC 5985 - HTTP-Enabled Location Delivery (HELD)
In case of success, the LIS replies with a locationResponse message, including a PIDF-LO document and/or one or more Location URIs. In the...
Read more >
RFC Receiver channel: failed to read funtionname from XML
XML to a flat file structure. Now in Runtime Workbench I get the following error: "MP: exception caught with cause com.sap.engine.interfaces.
Read more >
RFC 7807: Problem Details for HTTP APIs
RFC 7807 Problem Details March 2016 response body in a machine-readable format, the client can treat it appropriately; for example, triggering a transfer...
Read more >
Extensible Messaging and Presence Protocol (XMPP): Core
Resource Binding Namespace A.8. Stanza Error Namespace Appendix B. Contact Addresses Appendix C. Account Provisioning Appendix D. Differences from RFC 3920
Read more >
Language Guide (proto3) | Protocol Buffers - Google Developers
This guide describes how to use the protocol buffer language to structure your ... if you wanted to define the reply message format...
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