Skip to content

Commit

Permalink
Logs traceback errors on model (#18)
Browse files Browse the repository at this point in the history
* add errors field

* log exception in view

* Resets migration dependency and file name

Changes migration file and dependency back to the initial migration file. The project moved forward with a backwards incompatible change and this PR was not updated to reflect that change. All migrations were squashed into the initial migration for simplicity.

* Fixes incorrect indentation to pass lint check

Through user error, the indentation of this test method was incorrectly added. Likely, due to merging conflicts from the master branch using the GIthub UI.

* fix(): remove wrong param in traceback.format_exc (#39)

* Updates and renames migration file

Updating the order number of the file and the migration dependency resolves the migration conflict in the PR.
aschn authored and avelis committed Mar 1, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent b767aa0 commit 210511e
Showing 7 changed files with 55 additions and 0 deletions.
19 changes: 19 additions & 0 deletions rest_framework_tracking/migrations/0003_add_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
("rest_framework_tracking", "0002_auto_20170118_1713"),
]

operations = [
migrations.AddField(
'APIRequestLog',
'errors',
models.TextField(null=True, blank=True),
),
]
13 changes: 13 additions & 0 deletions rest_framework_tracking/mixins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .models import APIRequestLog
from django.utils.timezone import now
import traceback


class LoggingMixin(object):
@@ -71,6 +72,18 @@ def initial(self, request, *args, **kwargs):
finally:
self.request.log.save()

def handle_exception(self, exc):
# basic handling
response = super(LoggingMixin, self).handle_exception(exc)

# log error
self.request.log.errors = traceback.format_exc()
self.request.log.status_code = response.status_code
self.request.log.save()

# return
return response

def finalize_response(self, request, response, *args, **kwargs):
# regular finalize response
response = super(LoggingMixin, self).finalize_response(request, response, *args, **kwargs)
3 changes: 3 additions & 0 deletions rest_framework_tracking/models.py
Original file line number Diff line number Diff line change
@@ -41,6 +41,9 @@ class BaseAPIRequestLog(models.Model):
# response
response = models.TextField(null=True, blank=True)

# error traceback
errors = models.TextField(null=True, blank=True)

# status code
status_code = models.PositiveIntegerField(null=True, blank=True)

8 changes: 8 additions & 0 deletions tests/test_mixins.py
Original file line number Diff line number Diff line change
@@ -178,6 +178,14 @@ def test_log_request_404_error(self):
log = APIRequestLog.objects.first()
self.assertEqual(log.status_code, 404)
self.assertIn('Not found', log.response)
self.assertIn('Traceback', log.errors)

def test_log_request_500_error(self):
self.client.get('/500-error-logging')
log = APIRequestLog.objects.first()
self.assertEqual(log.status_code, 500)
self.assertIn('response', log.response)
self.assertIn('Traceback', log.errors)

def test_log_request_415_error(self):
content_type = 'text/plain'
5 changes: 5 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -76,6 +76,11 @@ def test_status_code(self):
status_code=200)
self.assertEqual(log.status_code, 200)

def test_errors(self):
log = APIRequestLog.objects.create(remote_addr=self.ip, requested_at=now(),
errors='dummy')
self.assertEqual(log.errors, 'dummy')

def test_queries_anon(self):
for i in range(100):
APIRequestLog.objects.create(remote_addr=self.ip, requested_at=now())
1 change: 1 addition & 0 deletions tests/urls.py
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
url(r'^json-logging$', test_views.MockJSONLoggingView.as_view()),
url(r'^validation-error-logging$', test_views.MockValidationErrorLoggingView.as_view()),
url(r'^404-error-logging$', test_views.Mock404ErrorLoggingView.as_view()),
url(r'^500-error-logging$', test_views.Mock500ErrorLoggingView.as_view()),
url(r'^415-error-logging$', test_views.Mock415ErrorLoggingView.as_view()),
url(r'^no-view-log$', test_views.MockNameAPIView.as_view()),
url(r'^view-log$', test_views.MockNameViewSet.as_view({'get': 'list'})),
6 changes: 6 additions & 0 deletions tests/views.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import serializers, viewsets, mixins
from rest_framework.exceptions import APIException
from rest_framework_tracking.mixins import LoggingMixin
from rest_framework_tracking.models import APIRequestLog
from tests.test_serializers import ApiRequestLogSerializer
@@ -71,6 +72,11 @@ def get(self, request):
return get_list_or_404(empty_qs)


class Mock500ErrorLoggingView(LoggingMixin, APIView):
def get(self, request):
raise APIException('response')


class Mock415ErrorLoggingView(LoggingMixin, APIView):
def post(self, request):
return request.data

0 comments on commit 210511e

Please sign in to comment.