1import flask
2
3from werkzeug.exceptions import HTTPException
4
5from ._http import HTTPStatus
6
7__all__ = (
8 "abort",
9 "RestError",
10 "ValidationError",
11 "SpecsError",
12)
13
14
15def abort(code=HTTPStatus.INTERNAL_SERVER_ERROR, message=None, **kwargs):
16 """
17 Properly abort the current request.
18
19 Raise a `HTTPException` for the given status `code`.
20 Attach any keyword arguments to the exception for later processing.
21
22 :param int code: The associated HTTP status code
23 :param str message: An optional details message
24 :param kwargs: Any additional data to pass to the error payload
25 :raise HTTPException:
26 """
27 try:
28 flask.abort(code)
29 except HTTPException as e:
30 if message:
31 kwargs["message"] = str(message)
32 if kwargs:
33 e.data = kwargs
34 raise
35
36
37class RestError(Exception):
38 """Base class for all Flask-RESTX Errors"""
39
40 def __init__(self, msg):
41 self.msg = msg
42
43 def __str__(self):
44 return self.msg
45
46
47class ValidationError(RestError):
48 """A helper class for validation errors."""
49
50 pass
51
52
53class SpecsError(RestError):
54 """A helper class for incoherent specifications."""
55
56 pass