2020-02-29 22:22:38 +00:00
|
|
|
from crc import ma, app
|
2019-12-27 18:50:03 +00:00
|
|
|
|
|
|
|
|
2020-02-10 21:19:23 +00:00
|
|
|
class ApiError(Exception):
|
2020-02-29 22:22:38 +00:00
|
|
|
def __init__(self, code, message, status_code=400):
|
|
|
|
self.status_code = status_code
|
2019-12-27 18:50:03 +00:00
|
|
|
self.code = code
|
|
|
|
self.message = message
|
2020-03-03 18:50:22 +00:00
|
|
|
Exception.__init__(self, self.message)
|
2019-12-27 18:50:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ApiErrorSchema(ma.Schema):
|
|
|
|
class Meta:
|
|
|
|
fields = ("code", "message")
|
|
|
|
|
2020-02-29 22:22:38 +00:00
|
|
|
|
|
|
|
@app.errorhandler(ApiError)
|
|
|
|
def handle_invalid_usage(error):
|
|
|
|
response = ApiErrorSchema().dump(error)
|
|
|
|
return response, error.status_code
|