Skip to content

Commit

Permalink
Fix merge conflicts for post collection.
Browse files Browse the repository at this point in the history
  • Loading branch information
kjohnson3 committed Apr 3, 2017
2 parents 613cac5 + d49d8e9 commit 9df7005
Show file tree
Hide file tree
Showing 6 changed files with 879 additions and 0 deletions.
27 changes: 27 additions & 0 deletions sqlalchemy_jsonapi/errors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from uuid import uuid4


Expand Down Expand Up @@ -137,3 +138,29 @@ def __init__(self, api_type):
tmpl = 'This backend has not been configured to handle resources of '\
'type {}.'
self.detail = tmpl.format(api_type)


def user_error(status_code, title, detail, pointer):
"""Create and return a general user error response that is jsonapi compliant.
Required args:
status_code: The HTTP status code associated with the problem.
title: A short summary of the problem.
detail: An explanation specific to the occurence of the problem.
pointer: The request path associated with the source of the problem.
"""
response = {
'errors': [{
'status': status_code,
'source': {'pointer': '{0}'.format(pointer)},
'title': title,
'detail': detail,
}],
'jsonapi': {
'version': '1.0'
},
'meta': {
'sqlalchemy_jsonapi_version': '4.0.9'
}
}
return json.dumps(response), status_code
37 changes: 37 additions & 0 deletions sqlalchemy_jsonapi/unittests/test_errors_user_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Test for error's user_error."""

import json
import unittest

from sqlalchemy_jsonapi import errors


class TestUserError(unittest.TestCase):
"""Tests for errors.user_error."""

def test_user_error(self):
"""Create user error succesfully."""
status_code = 400
title = 'User Error Occured'
detail = 'Testing user error'
pointer = '/test'

actual = errors.user_error(
status_code, title, detail, pointer)

data = {
'errors': [{
'status': status_code,
'source': {'pointer': '{0}'.format(pointer)},
'title': title,
'detail': detail,
}],
'jsonapi': {
'version': '1.0'
},
'meta': {
'sqlalchemy_jsonapi_version': '4.0.9'
}
}
expected = json.dumps(data), status_code
self.assertEqual(expected, actual)
Loading

0 comments on commit 9df7005

Please sign in to comment.