89 lines
2.2 KiB
Python
89 lines
2.2 KiB
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 NetwoApiErrorException(Exception):
|
|
"""
|
|
Exception thrown if Netwo API
|
|
"""
|
|
|
|
def __init__(self, description: str, netwo_status_code: int):
|
|
self.netwo_status_code = netwo_status_code
|
|
self.name = "Error contacting Netwo API"
|
|
self.description = description
|
|
|
|
|
|
class SwaggerValidationException(Exception):
|
|
"""
|
|
Exception thrown if API misused
|
|
"""
|
|
|
|
def __init__(self, err, data):
|
|
self.code = 400
|
|
self.name = "Error from Swagger API validation"
|
|
self.description = str(err)
|
|
self.data = data
|
|
|
|
|
|
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,
|
|
)
|
|
|
|
@self.flask_app.errorhandler(NetwoApiErrorException)
|
|
def handle_exception(e):
|
|
return (
|
|
jsonify(
|
|
{
|
|
"netwo_status_code": e.netwo_status_code,
|
|
"name": e.name,
|
|
"description": e.description,
|
|
}
|
|
),
|
|
500,
|
|
)
|
|
|
|
@self.flask_app.errorhandler(SwaggerValidationException)
|
|
def handle_exception(e):
|
|
return (
|
|
jsonify(
|
|
{
|
|
"code": e.code,
|
|
"name": e.name,
|
|
"description": e.description,
|
|
"data": e.data,
|
|
}
|
|
),
|
|
400,
|
|
)
|