Giter VIP home page Giter VIP logo

Comments (6)

pavlov99 avatar pavlov99 commented on June 16, 2024

Dear @cjerdonek

I have some concerns about that use case. As you mentioned, application might raise an exception, which is not under developer's control. If library would not validate the type, it might affect frontend developers later on. My suggestion would be to inherit from current serializer and inject it to jsonrpc manager.

from jsonrpc.utils import DatetimeDecimalEncoder

class MyEncoder(DatetimeDecimalEncoder):
    def default(self, o):
        try:
            return super(MyEncoder, self).default(self, o)
        except TypeError:
            return repr(o)

Then this encoder could be used here: https://github.com/pavlov99/json-rpc/blob/master/jsonrpc/backend/django.py#L62

Please let me know if that works.

from json-rpc.

cjerdonek avatar cjerdonek commented on June 16, 2024

@pavlov99 Thanks for such a prompt reply. It seems to me though that json-rpc has a responsibility to meet its guarantee and return valid JSON. Every application is susceptible to this issue. (Incidentally, it also seems that the issue isn't limited to the Django backend as you will see below.)

In this function, JSONRPCResponseManager constructs a JSONRPCServerError when an exception occurs. However, it makes the assumption that e.args is JSON serializable, which isn't necessarily the case. It seems json-rpc shouldn't be creating a JSONRPCError object there if it can't guarantee that the object is actually serializable.

from json-rpc.

cjerdonek avatar cjerdonek commented on June 16, 2024

Another suggestion: if you are concerned about backwards-forwards compatibility, etc, how about handling the serialization exception and raising a serializable exception saying something like, "unserializable error raised with type: ..."? That way developers will at least be alerted to the issue on a case-by-case basis, but as JSON as opposed to 500 response.

from json-rpc.

cjerdonek avatar cjerdonek commented on June 16, 2024

For the record, I just want to include this from the JSON-RPC 2.0 Specification, section 5 Response object:

When a rpc call is made, the Server MUST reply with a Response, except for in the case of Notifications. The Response is expressed as a single JSON Object, with the following members:

Thus, this issue is about a case in which json-rpc does not respond with a JSON object. Json-rpc should have some kind of global exception handler to ensure that it always returns a JSON response, regardless of the underlying application code.

from json-rpc.

pavlov99 avatar pavlov99 commented on June 16, 2024

@cjerdonek absolutely agree with the latest point. Let me add tests for Django 1.9.

from json-rpc.

arnuschky avatar arnuschky commented on June 16, 2024

I am facing the same problem. Any news on this?

from json-rpc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.