From ddcc8623d7d85699dc0e3a73080620f83079a6a8 Mon Sep 17 00:00:00 2001 From: Luke Lee Date: Thu, 4 Aug 2016 11:18:30 +0200 Subject: [PATCH 1/2] Skip django/flask tests if not installed - Django and Flask are not firm requirements set out in the setup.py so makes sense to not mark the tests as failed if they aren't installed. --- jsonrpc/tests/test_backend_django/tests.py | 9 +++++++-- jsonrpc/tests/test_backend_flask/tests.py | 8 ++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/jsonrpc/tests/test_backend_django/tests.py b/jsonrpc/tests/test_backend_django/tests.py index 3f8ff0e..664bfaa 100644 --- a/jsonrpc/tests/test_backend_django/tests.py +++ b/jsonrpc/tests/test_backend_django/tests.py @@ -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 diff --git a/jsonrpc/tests/test_backend_flask/tests.py b/jsonrpc/tests/test_backend_flask/tests.py index f57a7a1..5dd48ad 100644 --- a/jsonrpc/tests/test_backend_flask/tests.py +++ b/jsonrpc/tests/test_backend_flask/tests.py @@ -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 From 21d4ca753f80333d315870d613e959e318066f6d Mon Sep 17 00:00:00 2001 From: Piper Merriam Date: Fri, 2 Sep 2016 10:47:37 -0600 Subject: [PATCH 2/2] Add traceback data to error response when exceptions are raised --- jsonrpc/manager.py | 5 +++++ jsonrpc/tests/test_manager.py | 21 +++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/jsonrpc/manager.py b/jsonrpc/manager.py index 0dac0b5..7e83f4a 100644 --- a/jsonrpc/manager.py +++ b/jsonrpc/manager.py @@ -1,3 +1,5 @@ +import sys +import traceback import json import logging from .utils import is_invalid_params @@ -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): diff --git a/jsonrpc/tests/test_manager.py b/jsonrpc/tests/test_manager.py index 13a1287..221e9b2 100644 --- a/jsonrpc/tests/test_manager.py +++ b/jsonrpc/tests/test_manager.py @@ -1,3 +1,4 @@ +import os import sys if sys.version_info < (2, 7): import unittest2 as unittest @@ -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) @@ -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(