Skip to content
This repository has been archived by the owner on Jan 25, 2018. It is now read-only.

Commit

Permalink
Log JSON error responses from Solitude
Browse files Browse the repository at this point in the history
  • Loading branch information
kumar303 committed Dec 31, 2012
1 parent 01f6f88 commit 6e42a6e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
31 changes: 24 additions & 7 deletions webpay/base/middleware.py
@@ -1,14 +1,37 @@
import json
import pprint
import sys
import traceback

import commonware.log
from django.conf import settings
from django.utils.cache import patch_vary_headers
from django.utils.translation.trans_real import parse_accept_lang_header

import tower

log = commonware.log.getLogger('w.middleware')


class LogJSONerror:
"""
If the exception has JSON content, log the JSON message.
This is intended to catch and log Solitude error messages
such as form errors for 400 errors.
"""
def process_exception(self, request, exception):
etype = type(exception)
if hasattr(etype, '__name__'):
etype = etype.__name__
if hasattr(exception, 'content'):
try:
log.error('%s: %s: JSON: %s'
% (etype, exception,
json.loads(exception.content)))
except ValueError:
log.error('%s: %s: %s... (not JSON content)'
% (etype, exception,
str(exception.content)[0:50]))


class LogExceptionsMiddleware:
"""
Expand All @@ -18,12 +41,6 @@ class LogExceptionsMiddleware:
would not otherwise show the Django debug page.
"""
def process_exception(self, request, exception):
if hasattr(exception, 'content'):
try:
# Solitude JSON error.
pprint.pprint(json.loads(exception.content))
except Exception, exc:
print 'Could not load exception as JSON: %s' % exc
traceback.print_exception(*sys.exc_info())


Expand Down
23 changes: 22 additions & 1 deletion webpay/base/tests/test_middleware.py
@@ -1,3 +1,5 @@
import json

from django import http
from django.conf import settings
from django.test import TestCase
Expand All @@ -6,7 +8,7 @@
import fudge
from nose.tools import eq_

from webpay.base.middleware import LocaleMiddleware
from webpay.base.middleware import LocaleMiddleware, LogJSONerror


class TestLocaleMiddleware(TestCase):
Expand Down Expand Up @@ -68,3 +70,22 @@ def test_lang_non_existant(self):

def test_no_input(self):
eq_(self.process()[0], settings.LANGUAGE_CODE)


class ExcWithContent(Exception):

def __init__(self, msg, content):
Exception.__init__(self, msg)
self.content = content


class TestLogJSONerror(TestCase):

def test_json(self):
err = LogJSONerror()
exc = ExcWithContent('msg', json.dumps({'foo': 'bar'}))
err.process_exception(None, exc)

def test_not_json(self):
err = LogJSONerror()
err.process_exception(None, ExcWithContent('msg', '}]not valid JSON'))
1 change: 1 addition & 0 deletions webpay/settings/base.py
Expand Up @@ -139,6 +139,7 @@
'commonware.middleware.FrameOptionsHeader',
'mobility.middleware.DetectMobileMiddleware',
'mobility.middleware.XMobileMiddleware',
'webpay.base.middleware.LogJSONerror',
)

SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
Expand Down

0 comments on commit 6e42a6e

Please sign in to comment.