From e6b2a600cb93fee4552a69f0a1fdaf2b1d9f2b62 Mon Sep 17 00:00:00 2001 From: Andrew Melton Date: Tue, 18 Jun 2013 14:44:07 -0400 Subject: [PATCH] Adding exception logging to dbapi --- stacktach/dbapi.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/stacktach/dbapi.py b/stacktach/dbapi.py index 1a548ea..7b81430 100644 --- a/stacktach/dbapi.py +++ b/stacktach/dbapi.py @@ -33,9 +33,9 @@ from django.shortcuts import get_object_or_404 from stacktach import datetime_to_decimal as dt from stacktach import models +from stacktach import stacklog from stacktach import utils - DEFAULT_LIMIT = 50 HARD_LIMIT = 1000 @@ -68,6 +68,13 @@ def rsp(data): return HttpResponse(json.dumps(data), content_type="application/json") +def _log_api_exception(cls, ex, request): + line1 = "Exception: %s - %s - %s" % (cls.__name__, ex.status, ex.message) + line2 = "Request: %s - %s" % (request.method, request.path) + line3 = "Body: %s" % request.body + stacklog.error("%s/n%s/n%s" % (line1, line2, line3)) + + def api_call(func): @functools.wraps(func) @@ -75,12 +82,15 @@ def api_call(func): try: return rsp(func(*args, **kwargs)) except NotFoundException, e: + _log_api_exception(NotFoundException, e, args[0]) return HttpResponseNotFound(json.dumps(e.to_dict()), content_type="application/json") except BadRequestException, e: + _log_api_exception(BadRequestException, e, args[0]) return HttpResponseBadRequest(json.dumps(e.to_dict()), content_type="application/json") except APIException, e: + _log_api_exception(APIException, e, args[0]) return HttpResponseServerError(json.dumps(e.to_dict()), content_type="application/json")