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

HTTP status code of errors should be configurable #48

Open
slashingweapon opened this issue Aug 24, 2013 · 3 comments
Open

HTTP status code of errors should be configurable #48

slashingweapon opened this issue Aug 24, 2013 · 3 comments

Comments

@slashingweapon
Copy link

I generally don't believe in setting the HTTP status to anything other than 200 when errors are returned. I know some people will argue the point, and that's okay.

I think the default status code for jsonrpc.excetions.Error should be configurable from the settings file.

What I'm doing now is adding a decorator (after the jsonrpc_method decorator) that catches exceptions and remakes them into json-rpc exceptions with a 200 status.

def json_convert_exception(func):
    """When calling any function, catch any non-json-rpc error and make an OtherError from it."""
    def decorator(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except jsonrpc.exceptions.Error:
            raise   # re-raise the error if it was already a json-rpc error
        except Exception as ex:
            jsonEx = jsonrpc.exceptions.OtherError( str(ex) )
            if (hasattr(ex, 'code')):
                jsonEx.code = ex.code
            jsonEx.status = 200
            raise jsonEx, None, sys.exc_info()[2] # new exception, old stacktrace
    return decorator
@samuraisam
Copy link
Owner

Pull requests welcome. I don't have the time to write code for this right now.

@mreider
Copy link

mreider commented Dec 13, 2013

Can you show an example of how you use this with jsonrpc_method?

@slashingweapon
Copy link
Author

Here is a trivial example, showing the use of the json_convert_exception annotation.

@jsonrpc_method('send_event', site=mountpoint)
@json_convert_exception
def send_event_json(request, app, event, **kwargs):
    """do clever things here, and throw exceptions on errors"""
    return true

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

No branches or pull requests

3 participants