Giter VIP home page Giter VIP logo

Comments (8)

pavlov99 avatar pavlov99 commented on June 16, 2024

@deadbeef404 sure, let me see. I agree, TypeError inside the method would throw 'Invalid params' exception.

from json-rpc.

pavlov99 avatar pavlov99 commented on June 16, 2024

@deadbeef404 let us solve this issue from the business side.
Technically, it is impossible to distinguish TypeError because of invalid parameters and TypeError thrown from the function itself. Unfortunately, json-rpc could not do much about it, it does not know function it calls. What is possible to do:

  1. Catch TypeError from inside the function and throw JSONRPCDispatchException
    https://github.com/pavlov99/json-rpc/blob/master/jsonrpc/exceptions.py#L172
    From library side, need to document it better.
  2. I could add more information to error's data field, as implemented for general exceptions: https://github.com/pavlov99/json-rpc/blob/master/jsonrpc/manager.py#L109-114
    Would it solve your issue?
  3. Use *args, **kwargs for functions and accepts/returns decorators https://wiki.python.org/moin/PythonDecoratorLibrary#Type_Enforcement_.28accepts.2Freturns.29
    But this is the least desirable way.

Let me know what do you think about it.

from json-rpc.

pavlov99 avatar pavlov99 commented on June 16, 2024

I have implemented body message to TypeError (InvalidParams). https://github.com/pavlov99/json-rpc/blob/master/jsonrpc/manager.py#L106-110

It gives more information about the error: https://github.com/pavlov99/json-rpc/blob/master/jsonrpc/tests/test_manager.py#L82-91

Versionadded: 1.8.4

from json-rpc.

deadbeef404 avatar deadbeef404 commented on June 16, 2024

This change is in improvement as it would at lease preserve some information. There is the slight issue though: this is still is returning the wrong error code for the exception, and thus not matching the specification.

In regards to your point "it does not know what function it calls", well, it does. It has a reference to it to which it passes *request.args, **request.kwargs.

Taking this into account, the most ideal way would be to actually perform some type of argument matching by accessing method.func_code.co_varnames to inspect the call before it happens. Actually, this argument matching approach, if implemented, would best be run after the TypeError occurs, that way the code would not add the upfront overhead on every call, just those that have a TypeError already occurring.
Have you considered this approach?

from json-rpc.

deadbeef404 avatar deadbeef404 commented on June 16, 2024

For a better approach than the simple co_varnames, this seems useful: http://stackoverflow.com/a/197053/945856

from json-rpc.

pavlov99 avatar pavlov99 commented on June 16, 2024

@deadbeef404 point taken. I did not want to play with code inspection, but seems we could not avoid it. To follow specification is important and I agree with you that current error type is not correct in case of TypeError inside the function. Let me play with the code and add suggested functionality.

from json-rpc.

pavlov99 avatar pavlov99 commented on June 16, 2024

@deadbeef404 , updated code https://github.com/pavlov99/json-rpc/blob/master/jsonrpc/manager.py#L117-120
Version 1.9.0 has been released.

from json-rpc.

deadbeef404 avatar deadbeef404 commented on June 16, 2024

Thank you for your hard work! 👍

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.