Skip to content
This repository has been archived by the owner on Apr 11, 2022. It is now read-only.

Commit

Permalink
Add ProblemError
Browse files Browse the repository at this point in the history
  • Loading branch information
vbessonov committed Nov 8, 2020
1 parent 36977a1 commit ba04081
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions util/problem_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from flask_babel import LazyString
from nose.tools import set_trace

from core.exceptions import BaseError

JSON_MEDIA_TYPE = "application/api-problem+json"


Expand All @@ -20,6 +22,7 @@ def json(type, status, title, detail=None, instance=None, debug_message=None):
d['debug_message'] = debug_message
return j.dumps(d)


class ProblemDetail(object):

"""A common type of problem."""
Expand Down Expand Up @@ -79,3 +82,27 @@ def with_debug(self, debug_message, detail=None, status_code=None,
self.uri, status_code or self.status_code, title or self.title,
detail or self.detail, instance or self.instance, debug_message
)


class ProblemError(BaseError):
"""Exception class allowing to raise and catch ProblemDetail objects."""

def __init__(self, problem_detail):
"""Initialize a new instance of ProblemError class.
:param problem_detail: ProblemDetail object
:type problem_detail: ProblemDetail
"""
if not isinstance(problem_detail, ProblemDetail):
raise ValueError('Argument "problem_detail" must be an instance of ProblemDetail class')

self._problem_detail = problem_detail

@property
def problem_detail(self):
"""Return the ProblemDetail object associated with this exception.
:return: ProblemDetail object associated with this exception
:rtype: ProblemDetail
"""
return self._problem_detail

0 comments on commit ba04081

Please sign in to comment.