39 lines
890 B
Python
39 lines
890 B
Python
from flask import Flask, jsonify
|
|
|
|
|
|
class ApiParamException(Exception):
|
|
"""
|
|
Exception thrown if API misused
|
|
"""
|
|
|
|
def __init__(self, description):
|
|
self.code = 400
|
|
self.name = "Bad API parameter"
|
|
self.description = description
|
|
|
|
|
|
class FlaskExceptions:
|
|
"""
|
|
Manages flask custom exceptions
|
|
"""
|
|
|
|
def __init__(self, flask_app: Flask):
|
|
self.flask_app = flask_app
|
|
|
|
def add_exceptions(self):
|
|
"""
|
|
declares custom exceptions to flask app
|
|
"""
|
|
|
|
@self.flask_app.errorhandler(ApiParamException)
|
|
def handle_exception(e):
|
|
return (
|
|
jsonify(
|
|
{
|
|
"code": e.code,
|
|
"name": e.name,
|
|
"description": e.description,
|
|
}
|
|
),
|
|
400,
|
|
)
|