{"id":2119,"date":"2018-09-04T10:41:35","date_gmt":"2018-09-04T09:41:35","guid":{"rendered":"http:\/\/www.jurecuhalev.com\/blog\/?p=2119"},"modified":"2018-09-04T10:41:35","modified_gmt":"2018-09-04T09:41:35","slug":"logging-drf-serializer-errors-into-sentry","status":"publish","type":"post","link":"https:\/\/www.jurecuhalev.com\/blog\/logging-drf-serializer-errors-into-sentry\/","title":{"rendered":"Logging DRF Serializer errors into Sentry"},"content":{"rendered":"\n<p>For one of my Ember.js apps, I have a bit of a too complex Form flow. While I&#8217;m working on simplifying frontend, I wanted a way to easily log validation errors that users received. In addition to debugging, this helps me improve the labels and instructions on the form itself.<\/p>\n\n\n\n<p>Backend in this case if Django Request Framework driven with JSON API. The idea is to log all validation errors and redirect them to Sentry in Debug mode:<\/p>\n\n\n\n<p>First we declare a custom DRF Exception handler the uses JSON API exception handler and copies the data to sentry:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ exceptions.py\nfrom rest_framework_json_api.exceptions import exception_handler\nfrom raven.contrib.django.raven_compat.models import client\n\nfrom app.serializers import SubmissionSerializer\n\ndef custom_drf_exception_handler(exc, context):\n    response = exception_handler(exc, context)\n\n    if isinstance(context.get('view').get_serializer_class(), SubmissionSerializer):\n        client.captureMessage('Form Submission Validation Error', level='debug', extra=exc.get_full_details())\n\n\n    return response<\/code><\/pre>\n\n\n\n<p>And we also have to configure DRF to re-route errors through it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ settings.py\nREST_FRAMEWORK = {\n\t..\n\t'EXCEPTION_HANDLER': 'app.exceptions.custom_drf_exception_handler',\n\t..\n}<\/code><\/pre>\n\n\n\n<p>And that&#8217;s it. The end result is that when serialization error is triggered, we now get a nice error log in Sentry:<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><a href=\"https:\/\/www.jurecuhalev.com\/blog\/wp-content\/uploads\/2018\/09\/sentry.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.jurecuhalev.com\/blog\/wp-content\/uploads\/2018\/09\/sentry.png\" alt=\"\" class=\"wp-image-2122\" width=\"580\" height=\"271\" srcset=\"https:\/\/www.jurecuhalev.com\/blog\/wp-content\/uploads\/2018\/09\/sentry.png 1176w, https:\/\/www.jurecuhalev.com\/blog\/wp-content\/uploads\/2018\/09\/sentry-550x258.png 550w, https:\/\/www.jurecuhalev.com\/blog\/wp-content\/uploads\/2018\/09\/sentry-768x360.png 768w, https:\/\/www.jurecuhalev.com\/blog\/wp-content\/uploads\/2018\/09\/sentry-1024x480.png 1024w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><figcaption>Screenshot of validation errors from Sentry<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>For one of my Ember.js apps, I have a bit of a too complex Form flow. While I&#8217;m working on simplifying frontend, I wanted a way to easily log validation errors that users received. In addition to debugging, this helps me improve the labels and instructions on the form itself. Backend in this case if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-2119","post","type-post","status-publish","format-standard","hentry","category-ideas"],"acf":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts\/2119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/comments?post=2119"}],"version-history":[{"count":3,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts\/2119\/revisions"}],"predecessor-version":[{"id":2124,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts\/2119\/revisions\/2124"}],"wp:attachment":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/media?parent=2119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/categories?post=2119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/tags?post=2119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}