{"action":"created","comment":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/pulls/comments/48361701","id":48361701,"diff_hunk":"@@ -65,15 +73,61 @@ def __str__(self):\n # from rest_framework import serializers\n # raise serializers.ValidationError('Value was invalid')\n \n+ErrorDetails = namedtuple('ErrorDetails', ['message', 'code'])\n+\n+\n class ValidationError(APIException):\n     status_code = status.HTTP_400_BAD_REQUEST\n+    code = None\n \n-    def __init__(self, detail):\n-        # For validation errors the 'detail' key is always required.\n-        # The details should always be coerced to a list if not already.\n-        if not isinstance(detail, dict) and not isinstance(detail, list):\n-            detail = [detail]\n-        self.detail = _force_text_recursive(detail)\n+    def __init__(self, detail, code=None):\n+        if code:\n+            self.full_details = ErrorDetails(detail, code)\n+        else:\n+            self.full_details = detail","path":"rest_framework/exceptions.py","position":43,"original_position":43,"commit_id":"2344d2270a851acea6a3089b9d70b92df3fd7ee2","original_commit_id":"2344d2270a851acea6a3089b9d70b92df3fd7ee2","user":{"login":"johnraz","id":304164,"avatar_url":"https://avatars.githubusercontent.com/u/304164?v=3","gravatar_id":"","url":"https://api.github.com/users/johnraz","html_url":"https://github.com/johnraz","followers_url":"https://api.github.com/users/johnraz/followers","following_url":"https://api.github.com/users/johnraz/following{/other_user}","gists_url":"https://api.github.com/users/johnraz/gists{/gist_id}","starred_url":"https://api.github.com/users/johnraz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/johnraz/subscriptions","organizations_url":"https://api.github.com/users/johnraz/orgs","repos_url":"https://api.github.com/users/johnraz/repos","events_url":"https://api.github.com/users/johnraz/events{/privacy}","received_events_url":"https://api.github.com/users/johnraz/received_events","type":"User","site_admin":false},"body":"What do you mean ? Above or below ? ","created_at":"2015-12-23T17:01:29Z","updated_at":"2015-12-23T17:01:29Z","html_url":"https://github.com/tomchristie/django-rest-framework/pull/3716#discussion_r48361701","pull_request_url":"https://api.github.com/repos/tomchristie/django-rest-framework/pulls/3716","_links":{"self":{"href":"https://api.github.com/repos/tomchristie/django-rest-framework/pulls/comments/48361701"},"html":{"href":"https://github.com/tomchristie/django-rest-framework/pull/3716#discussion_r48361701"},"pull_request":{"href":"https://api.github.com/repos/tomchristie/django-rest-framework/pulls/3716"}}},"pull_request":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/pulls/3716","id":52973039,"html_url":"https://github.com/tomchristie/django-rest-framework/pull/3716","diff_url":"https://github.com/tomchristie/django-rest-framework/pull/3716.diff","patch_url":"https://github.com/tomchristie/django-rest-framework/pull/3716.patch","issue_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/3716","number":3716,"state":"open","locked":false,"title":"Introduce error code for validation errors.","user":{"login":"johnraz","id":304164,"avatar_url":"https://avatars.githubusercontent.com/u/304164?v=3","gravatar_id":"","url":"https://api.github.com/users/johnraz","html_url":"https://github.com/johnraz","followers_url":"https://api.github.com/users/johnraz/followers","following_url":"https://api.github.com/users/johnraz/following{/other_user}","gists_url":"https://api.github.com/users/johnraz/gists{/gist_id}","starred_url":"https://api.github.com/users/johnraz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/johnraz/subscriptions","organizations_url":"https://api.github.com/users/johnraz/orgs","repos_url":"https://api.github.com/users/johnraz/repos","events_url":"https://api.github.com/users/johnraz/events{/privacy}","received_events_url":"https://api.github.com/users/johnraz/received_events","type":"User","site_admin":false},"body":"This patch is meant to fix #3111, regarding comments made to #3137\r\nand #3169.\r\n\r\nThe `ValidationError` will now contain a `code` attribute.\r\n\r\nBefore this patch, `ValidationError.detail` only contained a\r\n`dict` with values equal to a  `list` of string error messages or\r\ndirectly a `list` containing string error messages.\r\n\r\nNow, the string error messages are replaced with `ValidationError`.\r\n\r\nThis means that, depending on the case, you will not only get a\r\nstring back but a full object containing both the error message and\r\nthe error code, respectively `ValidationError.detail` and\r\n`ValidationError.code`.\r\n\r\nIt is important to note that the `code` attribute is not relevant\r\nwhen the `ValidationError` represents a combination of errors and\r\nhence is `None` in such cases.\r\n\r\nThe main benefit of this change is that the error message and\r\nerror code are now accessible in the custom exception handler and can\r\nbe used to format the error response.\r\n\r\nA custom exception handler example is available in the\r\n`TestValidationErrorWithCode` test.\r\n\r\nRegarding the other discussion:\r\n- This PR doesn't require any new settings\r\n- The `build_error` method was dropped\r\n- The `build_error_from_django_validation_error` has been kept because this is the only way to \"convert\" a regular `DjangoValidationError ` to a djrf `ValidationError`. \r\n\r\n@tomchristie @jpadilla @qsorix: if you don't mind giving this a look ;-)","created_at":"2015-12-08T12:15:11Z","updated_at":"2015-12-23T17:01:29Z","closed_at":null,"merged_at":null,"merge_commit_sha":"276178210e5915fb912944ea4e24abef5318eca9","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/tomchristie/django-rest-framework/pulls/3716/commits","review_comments_url":"https://api.github.com/repos/tomchristie/django-rest-framework/pulls/3716/comments","review_comment_url":"https://api.github.com/repos/tomchristie/django-rest-framework/pulls/comments{/number}","comments_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/3716/comments","statuses_url":"https://api.github.com/repos/tomchristie/django-rest-framework/statuses/2344d2270a851acea6a3089b9d70b92df3fd7ee2","head":{"label":"johnraz:validation-error-codes","ref":"validation-error-codes","sha":"2344d2270a851acea6a3089b9d70b92df3fd7ee2","user":{"login":"johnraz","id":304164,"avatar_url":"https://avatars.githubusercontent.com/u/304164?v=3","gravatar_id":"","url":"https://api.github.com/users/johnraz","html_url":"https://github.com/johnraz","followers_url":"https://api.github.com/users/johnraz/followers","following_url":"https://api.github.com/users/johnraz/following{/other_user}","gists_url":"https://api.github.com/users/johnraz/gists{/gist_id}","starred_url":"https://api.github.com/users/johnraz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/johnraz/subscriptions","organizations_url":"https://api.github.com/users/johnraz/orgs","repos_url":"https://api.github.com/users/johnraz/repos","events_url":"https://api.github.com/users/johnraz/events{/privacy}","received_events_url":"https://api.github.com/users/johnraz/received_events","type":"User","site_admin":false},"repo":{"id":47538509,"name":"django-rest-framework","full_name":"johnraz/django-rest-framework","owner":{"login":"johnraz","id":304164,"avatar_url":"https://avatars.githubusercontent.com/u/304164?v=3","gravatar_id":"","url":"https://api.github.com/users/johnraz","html_url":"https://github.com/johnraz","followers_url":"https://api.github.com/users/johnraz/followers","following_url":"https://api.github.com/users/johnraz/following{/other_user}","gists_url":"https://api.github.com/users/johnraz/gists{/gist_id}","starred_url":"https://api.github.com/users/johnraz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/johnraz/subscriptions","organizations_url":"https://api.github.com/users/johnraz/orgs","repos_url":"https://api.github.com/users/johnraz/repos","events_url":"https://api.github.com/users/johnraz/events{/privacy}","received_events_url":"https://api.github.com/users/johnraz/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/johnraz/django-rest-framework","description":"Web APIs for Django.","fork":true,"url":"https://api.github.com/repos/johnraz/django-rest-framework","forks_url":"https://api.github.com/repos/johnraz/django-rest-framework/forks","keys_url":"https://api.github.com/repos/johnraz/django-rest-framework/keys{/key_id}","collaborators_url":"https://api.github.com/repos/johnraz/django-rest-framework/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/johnraz/django-rest-framework/teams","hooks_url":"https://api.github.com/repos/johnraz/django-rest-framework/hooks","issue_events_url":"https://api.github.com/repos/johnraz/django-rest-framework/issues/events{/number}","events_url":"https://api.github.com/repos/johnraz/django-rest-framework/events","assignees_url":"https://api.github.com/repos/johnraz/django-rest-framework/assignees{/user}","branches_url":"https://api.github.com/repos/johnraz/django-rest-framework/branches{/branch}","tags_url":"https://api.github.com/repos/johnraz/django-rest-framework/tags","blobs_url":"https://api.github.com/repos/johnraz/django-rest-framework/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/johnraz/django-rest-framework/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/johnraz/django-rest-framework/git/refs{/sha}","trees_url":"https://api.github.com/repos/johnraz/django-rest-framework/git/trees{/sha}","statuses_url":"https://api.github.com/repos/johnraz/django-rest-framework/statuses/{sha}","languages_url":"https://api.github.com/repos/johnraz/django-rest-framework/languages","stargazers_url":"https://api.github.com/repos/johnraz/django-rest-framework/stargazers","contributors_url":"https://api.github.com/repos/johnraz/django-rest-framework/contributors","subscribers_url":"https://api.github.com/repos/johnraz/django-rest-framework/subscribers","subscription_url":"https://api.github.com/repos/johnraz/django-rest-framework/subscription","commits_url":"https://api.github.com/repos/johnraz/django-rest-framework/commits{/sha}","git_commits_url":"https://api.github.com/repos/johnraz/django-rest-framework/git/commits{/sha}","comments_url":"https://api.github.com/repos/johnraz/django-rest-framework/comments{/number}","issue_comment_url":"https://api.github.com/repos/johnraz/django-rest-framework/issues/comments{/number}","contents_url":"https://api.github.com/repos/johnraz/django-rest-framework/contents/{+path}","compare_url":"https://api.github.com/repos/johnraz/django-rest-framework/compare/{base}...{head}","merges_url":"https://api.github.com/repos/johnraz/django-rest-framework/merges","archive_url":"https://api.github.com/repos/johnraz/django-rest-framework/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/johnraz/django-rest-framework/downloads","issues_url":"https://api.github.com/repos/johnraz/django-rest-framework/issues{/number}","pulls_url":"https://api.github.com/repos/johnraz/django-rest-framework/pulls{/number}","milestones_url":"https://api.github.com/repos/johnraz/django-rest-framework/milestones{/number}","notifications_url":"https://api.github.com/repos/johnraz/django-rest-framework/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/johnraz/django-rest-framework/labels{/name}","releases_url":"https://api.github.com/repos/johnraz/django-rest-framework/releases{/id}","created_at":"2015-12-07T08:14:07Z","updated_at":"2015-12-07T08:14:09Z","pushed_at":"2015-12-22T15:13:55Z","git_url":"git://github.com/johnraz/django-rest-framework.git","ssh_url":"git@github.com:johnraz/django-rest-framework.git","clone_url":"https://github.com/johnraz/django-rest-framework.git","svn_url":"https://github.com/johnraz/django-rest-framework","homepage":"www.django-rest-framework.org","size":21823,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"tomchristie:master","ref":"master","sha":"503687d86862c538b3aba8bcb7a980671f23156d","user":{"login":"tomchristie","id":647359,"avatar_url":"https://avatars.githubusercontent.com/u/647359?v=3","gravatar_id":"","url":"https://api.github.com/users/tomchristie","html_url":"https://github.com/tomchristie","followers_url":"https://api.github.com/users/tomchristie/followers","following_url":"https://api.github.com/users/tomchristie/following{/other_user}","gists_url":"https://api.github.com/users/tomchristie/gists{/gist_id}","starred_url":"https://api.github.com/users/tomchristie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tomchristie/subscriptions","organizations_url":"https://api.github.com/users/tomchristie/orgs","repos_url":"https://api.github.com/users/tomchristie/repos","events_url":"https://api.github.com/users/tomchristie/events{/privacy}","received_events_url":"https://api.github.com/users/tomchristie/received_events","type":"User","site_admin":false},"repo":{"id":1431547,"name":"django-rest-framework","full_name":"tomchristie/django-rest-framework","owner":{"login":"tomchristie","id":647359,"avatar_url":"https://avatars.githubusercontent.com/u/647359?v=3","gravatar_id":"","url":"https://api.github.com/users/tomchristie","html_url":"https://github.com/tomchristie","followers_url":"https://api.github.com/users/tomchristie/followers","following_url":"https://api.github.com/users/tomchristie/following{/other_user}","gists_url":"https://api.github.com/users/tomchristie/gists{/gist_id}","starred_url":"https://api.github.com/users/tomchristie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tomchristie/subscriptions","organizations_url":"https://api.github.com/users/tomchristie/orgs","repos_url":"https://api.github.com/users/tomchristie/repos","events_url":"https://api.github.com/users/tomchristie/events{/privacy}","received_events_url":"https://api.github.com/users/tomchristie/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/tomchristie/django-rest-framework","description":"Web APIs for Django.","fork":false,"url":"https://api.github.com/repos/tomchristie/django-rest-framework","forks_url":"https://api.github.com/repos/tomchristie/django-rest-framework/forks","keys_url":"https://api.github.com/repos/tomchristie/django-rest-framework/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tomchristie/django-rest-framework/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tomchristie/django-rest-framework/teams","hooks_url":"https://api.github.com/repos/tomchristie/django-rest-framework/hooks","issue_events_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/events{/number}","events_url":"https://api.github.com/repos/tomchristie/django-rest-framework/events","assignees_url":"https://api.github.com/repos/tomchristie/django-rest-framework/assignees{/user}","branches_url":"https://api.github.com/repos/tomchristie/django-rest-framework/branches{/branch}","tags_url":"https://api.github.com/repos/tomchristie/django-rest-framework/tags","blobs_url":"https://api.github.com/repos/tomchristie/django-rest-framework/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tomchristie/django-rest-framework/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tomchristie/django-rest-framework/git/refs{/sha}","trees_url":"https://api.github.com/repos/tomchristie/django-rest-framework/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tomchristie/django-rest-framework/statuses/{sha}","languages_url":"https://api.github.com/repos/tomchristie/django-rest-framework/languages","stargazers_url":"https://api.github.com/repos/tomchristie/django-rest-framework/stargazers","contributors_url":"https://api.github.com/repos/tomchristie/django-rest-framework/contributors","subscribers_url":"https://api.github.com/repos/tomchristie/django-rest-framework/subscribers","subscription_url":"https://api.github.com/repos/tomchristie/django-rest-framework/subscription","commits_url":"https://api.github.com/repos/tomchristie/django-rest-framework/commits{/sha}","git_commits_url":"https://api.github.com/repos/tomchristie/django-rest-framework/git/commits{/sha}","comments_url":"https://api.github.com/repos/tomchristie/django-rest-framework/comments{/number}","issue_comment_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/comments{/number}","contents_url":"https://api.github.com/repos/tomchristie/django-rest-framework/contents/{+path}","compare_url":"https://api.github.com/repos/tomchristie/django-rest-framework/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tomchristie/django-rest-framework/merges","archive_url":"https://api.github.com/repos/tomchristie/django-rest-framework/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tomchristie/django-rest-framework/downloads","issues_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues{/number}","pulls_url":"https://api.github.com/repos/tomchristie/django-rest-framework/pulls{/number}","milestones_url":"https://api.github.com/repos/tomchristie/django-rest-framework/milestones{/number}","notifications_url":"https://api.github.com/repos/tomchristie/django-rest-framework/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tomchristie/django-rest-framework/labels{/name}","releases_url":"https://api.github.com/repos/tomchristie/django-rest-framework/releases{/id}","created_at":"2011-03-02T17:13:56Z","updated_at":"2015-12-22T16:49:30Z","pushed_at":"2015-12-23T16:58:30Z","git_url":"git://github.com/tomchristie/django-rest-framework.git","ssh_url":"git@github.com:tomchristie/django-rest-framework.git","clone_url":"https://github.com/tomchristie/django-rest-framework.git","svn_url":"https://github.com/tomchristie/django-rest-framework","homepage":"www.django-rest-framework.org","size":26700,"stargazers_count":5063,"watchers_count":5063,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":1720,"mirror_url":null,"open_issues_count":158,"forks":1720,"open_issues":158,"watchers":5063,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/tomchristie/django-rest-framework/pulls/3716"},"html":{"href":"https://github.com/tomchristie/django-rest-framework/pull/3716"},"issue":{"href":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/3716"},"comments":{"href":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/3716/comments"},"review_comments":{"href":"https://api.github.com/repos/tomchristie/django-rest-framework/pulls/3716/comments"},"review_comment":{"href":"https://api.github.com/repos/tomchristie/django-rest-framework/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/tomchristie/django-rest-framework/pulls/3716/commits"},"statuses":{"href":"https://api.github.com/repos/tomchristie/django-rest-framework/statuses/2344d2270a851acea6a3089b9d70b92df3fd7ee2"}}}}