Skip to content

Instantly share code, notes, and snippets.

@uniqueg
Last active May 24, 2020 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uniqueg/f056c93826baef7f3c89365f4f2b130e to your computer and use it in GitHub Desktop.
Save uniqueg/f056c93826baef7f3c89365f4f2b130e to your computer and use it in GitHub Desktop.
Custom errors, handlers and corresponding unit tests for Connexion apps
def __handle_task_not_found(exception: Exception) -> Response:
    return Response(
        response=dumps({
            'msg': 'The requested task was not found.',
            'status_code': '404'
            }),
        status=404,
        mimetype="application/problem+json"
    )
def test_task_not_found():
    """Test for functioning of custom handler for task not found"""
    error = __handle_task_not_found(Exception)
    assert error.status == '404 NOT FOUND'
    assert error.mimetype == "application/problem+json"
    response = json.loads(error.data.decode('utf-8'))
    assert response == {
        "msg": "The requested task was not found.",
        "status_code": "404"
        }
# CUSTOM ERRORS
class ResourceNotFound(ProblemException, NotFound, BaseException):
    """ResourceNotFound(404) error compatible with Connexion."""

    def __init__(self, title=None, **kwargs):
        super(ResourceNotFound, self).__init__(title=title, **kwargs)
def test_object_creation():
    obj = ResourceNotFound()
    assert obj.code == 404
    assert obj.description == (
            "The requested URL was not found on the server. "
            "If you entered the URL manually please check your "
            "spelling and try again."
    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment