-
Notifications
You must be signed in to change notification settings - Fork 0
/
ourexceptions.py
59 lines (40 loc) · 1.31 KB
/
ourexceptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Copyright (C) Mark Breedlove and Qingyang Xi
# See README.md and License.txt.
__all__ = ['BadRequestError', 'ConflictError', 'ForbiddenError',
'UnauthorizedError', 'NotFoundError']
from flask import jsonify
from app import app
class HTTPError(Exception):
status_code = 500
message = 'Internal Server Error'
def __init__(self, message=None, status_code=None, payload=None):
Exception.__init__(self)
if message is not None:
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload
def to_dict(self):
rv = dict(self.payload or ())
rv['message'] = self.message
return rv
class BadRequestError(HTTPError):
status_code = 400
message = 'Bad Request'
class UnauthorizedError(HTTPError):
status_code = 401
message = 'Unauthorized'
class ForbiddenError(HTTPError):
status_code = 403
message = 'Forbidden'
class NotFoundError(HTTPError):
status_code = 404
message = 'Not Found'
class ConflictError(HTTPError):
status_code = 409
message = 'Conflict'
@app.errorhandler(HTTPError)
def handle_http_error(error):
response = jsonify(error.to_dict())
response.status = "%s %s" % (error.status_code, error.message)
return response