majuna/app/api/__init__.py

43 lines
1.2 KiB
Python
Raw Normal View History

from flask import Blueprint, jsonify
from flask.typing import ResponseReturnValue
from werkzeug.exceptions import HTTPException
2024-11-10 13:38:51 +00:00
2024-12-06 13:34:44 +00:00
from app.api.onion import api_onion
from app.api.web import api_web
2024-11-10 13:38:51 +00:00
2024-12-06 18:15:47 +00:00
api = Blueprint("api", __name__)
api.register_blueprint(api_onion, url_prefix="/onion")
api.register_blueprint(api_web, url_prefix="/web")
2024-11-10 13:38:51 +00:00
@api.errorhandler(400)
def bad_request(error: HTTPException) -> ResponseReturnValue:
2024-12-06 18:15:47 +00:00
response = jsonify({"error": "Bad Request", "message": error.description})
2024-11-10 13:38:51 +00:00
response.status_code = 400
return response
@api.errorhandler(401)
def unauthorized(error: HTTPException) -> ResponseReturnValue:
2024-12-06 18:15:47 +00:00
response = jsonify({"error": "Unauthorized", "message": error.description})
2024-11-10 13:38:51 +00:00
response.status_code = 401
return response
@api.errorhandler(404)
def not_found(_: HTTPException) -> ResponseReturnValue:
2024-12-06 18:15:47 +00:00
response = jsonify(
{"error": "Not found", "message": "Resource could not be found."}
)
2024-11-10 13:38:51 +00:00
response.status_code = 404
return response
@api.errorhandler(500)
def internal_server_error(_: HTTPException) -> ResponseReturnValue:
2024-12-06 18:15:47 +00:00
response = jsonify(
{"error": "Internal Server Error", "message": "An unexpected error occurred."}
)
2024-11-10 13:38:51 +00:00
response.status_code = 500
return response