2021-11-02 08:54:07 +01:00
|
|
|
from flask import jsonify
|
|
|
|
from jsonschema import ValidationError
|
|
|
|
|
|
|
|
|
|
|
|
class KratosError(Exception):
|
|
|
|
pass
|
|
|
|
|
2022-11-08 09:52:45 +01:00
|
|
|
class AuthentikError(Exception):
|
|
|
|
pass
|
2021-11-02 08:54:07 +01:00
|
|
|
|
2022-01-18 10:48:18 +01:00
|
|
|
class HydraError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-11-02 08:54:07 +01:00
|
|
|
class BadRequest(Exception):
|
|
|
|
pass
|
|
|
|
|
2022-06-09 12:21:47 +02:00
|
|
|
class Unauthorized(Exception):
|
|
|
|
pass
|
2021-11-02 08:54:07 +01:00
|
|
|
|
|
|
|
def bad_request_error(e):
|
|
|
|
message = e.args[0] if e.args else "Bad request to the server."
|
2021-11-16 12:04:40 +01:00
|
|
|
return jsonify({"errorMessage": message}), 400
|
2021-11-02 08:54:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def validation_error(e):
|
|
|
|
original_error = e.description
|
|
|
|
return (
|
|
|
|
jsonify({"errorMessage": "{} is not valid.".format(original_error.path[0])}),
|
|
|
|
400,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def kratos_error(e):
|
2022-01-18 10:48:18 +01:00
|
|
|
message = "[KratosError] " + e.args[0] if e.args else "Failed to contact Kratos."
|
|
|
|
status_code = e.args[1] if e.args else 500
|
|
|
|
return jsonify({"errorMessage": message}), status_code
|
|
|
|
|
|
|
|
|
|
|
|
def hydra_error(e):
|
|
|
|
message = "[HydraError] " + e.args[0] if e.args else "Failed to contact Hydra."
|
2021-11-02 08:54:07 +01:00
|
|
|
status_code = e.args[1] if e.args else 500
|
|
|
|
return jsonify({"errorMessage": message}), status_code
|
|
|
|
|
|
|
|
|
|
|
|
def global_error(e):
|
2021-11-16 12:04:40 +01:00
|
|
|
message = str(e)
|
2022-01-18 10:48:18 +01:00
|
|
|
return jsonify({"errorMessage": message}), 500
|
2022-06-09 12:21:47 +02:00
|
|
|
|
|
|
|
def unauthorized_error(e):
|
|
|
|
message = str(e)
|
2022-06-09 12:26:50 +02:00
|
|
|
return jsonify({"errorMessage": message}), 403
|