Adding exception logging to dbapi

This commit is contained in:
Andrew Melton 2013-06-18 14:44:07 -04:00
parent 0aef1307b9
commit e6b2a600cb

View File

@ -33,9 +33,9 @@ from django.shortcuts import get_object_or_404
from stacktach import datetime_to_decimal as dt from stacktach import datetime_to_decimal as dt
from stacktach import models from stacktach import models
from stacktach import stacklog
from stacktach import utils from stacktach import utils
DEFAULT_LIMIT = 50 DEFAULT_LIMIT = 50
HARD_LIMIT = 1000 HARD_LIMIT = 1000
@ -68,6 +68,13 @@ def rsp(data):
return HttpResponse(json.dumps(data), content_type="application/json") 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): def api_call(func):
@functools.wraps(func) @functools.wraps(func)
@ -75,12 +82,15 @@ def api_call(func):
try: try:
return rsp(func(*args, **kwargs)) return rsp(func(*args, **kwargs))
except NotFoundException, e: except NotFoundException, e:
_log_api_exception(NotFoundException, e, args[0])
return HttpResponseNotFound(json.dumps(e.to_dict()), return HttpResponseNotFound(json.dumps(e.to_dict()),
content_type="application/json") content_type="application/json")
except BadRequestException, e: except BadRequestException, e:
_log_api_exception(BadRequestException, e, args[0])
return HttpResponseBadRequest(json.dumps(e.to_dict()), return HttpResponseBadRequest(json.dumps(e.to_dict()),
content_type="application/json") content_type="application/json")
except APIException, e: except APIException, e:
_log_api_exception(APIException, e, args[0])
return HttpResponseServerError(json.dumps(e.to_dict()), return HttpResponseServerError(json.dumps(e.to_dict()),
content_type="application/json") content_type="application/json")