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

Add traceback data to error response when exceptions are raised #50

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions jsonrpc/manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys
import traceback
import json
import logging
from .utils import is_invalid_params
Expand Down Expand Up @@ -113,6 +115,9 @@ def response(**kwargs):
"type": e.__class__.__name__,
"args": e.args,
"message": str(e),
"traceback": ''.join(
traceback.format_exception(*sys.exc_info())
),
}
if isinstance(e, TypeError) and is_invalid_params(
method, *request.args, **request.kwargs):
Expand Down
9 changes: 7 additions & 2 deletions jsonrpc/tests/test_backend_django/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
from __future__ import absolute_import
import os

from django.core.urlresolvers import RegexURLPattern
from django.test import TestCase
try:
from django.core.urlresolvers import RegexURLPattern
from django.test import TestCase
except ImportError:
import unittest
raise unittest.SkipTest('Django not found for testing')

from ...backend.django import JSONRPCAPI, api
import json

Expand Down
8 changes: 6 additions & 2 deletions jsonrpc/tests/test_backend_flask/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
else:
import unittest

# Flask is supported only for python2 and pyton3.3+
# Flask is supported only for python2 and python3.3+
if sys.version_info < (3, 0) or sys.version_info >= (3, 3):
from flask import Flask
try:
from flask import Flask
except ImportError:
raise unittest.SkipTest('Flask not found for testing')

from ...backend.flask import JSONRPCAPI, api

@api.dispatcher.add_method
Expand Down
21 changes: 11 additions & 10 deletions jsonrpc/tests/test_manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
if sys.version_info < (2, 7):
import unittest2 as unittest
Expand Down Expand Up @@ -122,11 +123,11 @@ def test_server_error(self):
self.assertTrue(isinstance(response, JSONRPC20Response))
self.assertEqual(response.error["message"], "Server error")
self.assertEqual(response.error["code"], -32000)
self.assertEqual(response.error["data"], {
"type": "KeyError",
"args": ('error_explanation',),
"message": "'error_explanation'",
})
self.assertEqual(response.error["data"]['type'], "KeyError")
self.assertEqual(response.error["data"]['args'], ('error_explanation',))
self.assertEqual(response.error["data"]['message'], "'error_explanation'")
self.assertIn('traceback', response.error["data"])
self.assertIn(os.path.basename(__file__), response.error["data"]['traceback'])

def test_notification_calls_method(self):
request = JSONRPC20Request("long_time_method", is_notification=True)
Expand Down Expand Up @@ -155,11 +156,11 @@ def test_type_error_inside_method(self):
self.assertTrue(isinstance(response, JSONRPC20Response))
self.assertEqual(response.error["message"], "Server error")
self.assertEqual(response.error["code"], -32000)
self.assertEqual(response.error["data"], {
"type": "TypeError",
"args": ('TypeError inside method',),
"message": 'TypeError inside method',
})
self.assertEqual(response.error["data"]['type'], "TypeError")
self.assertEqual(response.error["data"]['args'], ('TypeError inside method',))
self.assertEqual(response.error["data"]['message'], 'TypeError inside method')
self.assertIn('traceback', response.error["data"])
self.assertIn(os.path.basename(__file__), response.error["data"]['traceback'])

def test_invalid_params_before_dispatcher_error(self):
request = JSONRPC20Request(
Expand Down