[add]: Error handling
This commit is contained in:
@@ -131,3 +131,4 @@ TypeError: require_api_key() missing 1 required positional argument: 'f'
|
||||
2025-03-21 09:41:13,582 - ERROR - Unauthorized access: Missing App-ID.
|
||||
2025-03-21 09:41:16,993 - INFO - Processing Disbursement request
|
||||
2025-03-21 09:41:16,995 - ERROR - Validation Error: {'requestId': ['Missing data for required field.'], 'productId': ['Missing data for required field.'], 'collectAmountMgtFee': ['Missing data for required field.'], 'collectAmountVAT': ['Missing data for required field.'], 'provideAmount': ['Missing data for required field.'], 'countryId': ['Missing data for required field.'], 'debtId': ['Missing data for required field.'], 'collectAmountInsurance': ['Missing data for required field.'], 'channel': ['Unknown field.'], 'lienAmount': ['Unknown field.'], '$type': ['Unknown field.'], 'msisdn': ['Unknown field.'], 'countryCode': ['Unknown field.']}
|
||||
2025-03-21 09:48:19,845 - ERROR - Unauthorized access: Missing API key.
|
||||
|
||||
@@ -2,6 +2,7 @@ from flask import Flask
|
||||
from flask_cors import CORS
|
||||
from app.config import Config
|
||||
from app.routes import api
|
||||
from app.errors import bad_request, method_not_allowed, not_found
|
||||
|
||||
def create_app():
|
||||
""" Factory function to create a Flask app instance """
|
||||
@@ -16,4 +17,10 @@ def create_app():
|
||||
# Register blueprints
|
||||
app.register_blueprint(api)
|
||||
|
||||
|
||||
# Error Handlers
|
||||
app.register_error_handler(400, bad_request)
|
||||
app.register_error_handler(404, not_found)
|
||||
app.register_error_handler(405, method_not_allowed)
|
||||
|
||||
return app
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ import os
|
||||
class Config:
|
||||
"""Base configuration for Flask app"""
|
||||
|
||||
# SQLALCHEMY_DATABASE_URI = "mysql://root:password@localhost/flask_app"
|
||||
# SQLALCHEMY_DATABASE_URI =os.environ.get("DATABASE_URL", "database_url")
|
||||
# SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||
# SECRET_KEY = os.environ.get("SECRET_KEY", "your_secret_key")
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
from .handlers import bad_request, method_not_allowed, not_found
|
||||
@@ -0,0 +1,11 @@
|
||||
from flask import jsonify
|
||||
from app.helpers.response_helper import ResponseHelper
|
||||
|
||||
def method_not_allowed(error):
|
||||
return ResponseHelper.method_not_allowed(message="Method Not Allowed")
|
||||
|
||||
def not_found(error):
|
||||
return ResponseHelper.not_found(message="URL Not Found")
|
||||
|
||||
def bad_request(error):
|
||||
return ResponseHelper.bad_request(message="Bad Request")
|
||||
@@ -212,4 +212,40 @@ class ResponseHelper:
|
||||
"""
|
||||
return ResponseHelper.build_response(False, message, data, 422, error)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def method_not_allowed(
|
||||
message: str = "Method Not Allowed",
|
||||
data: Optional[Union[Dict, List, str]] = None,
|
||||
error: Optional[Union[Dict, str]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Return a response for a method not allowed error.
|
||||
|
||||
Args:
|
||||
message (str): A message describing the error.
|
||||
data (Optional[Union[Dict, List, str]]): The data to return in the response.
|
||||
error (Optional[Union[Dict, str]]): Any error details to include in the response.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary representing the JSON response.
|
||||
"""
|
||||
return ResponseHelper.build_response(False, message, data, 405, error)
|
||||
|
||||
@staticmethod
|
||||
def bad_request(
|
||||
message: str = "Bad Request",
|
||||
data: Optional[Union[Dict, List, str]] = None,
|
||||
error: Optional[Union[Dict, str]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Return a response for a bad request error.
|
||||
|
||||
Args:
|
||||
message (str): A message describing the error.
|
||||
data (Optional[Union[Dict, List, str]]): The data to return in the response.
|
||||
error (Optional[Union[Dict, str]]): Any error details to include in the response.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary representing the JSON response.
|
||||
"""
|
||||
return ResponseHelper.build_response(False, message, data, 400, error)
|
||||
@@ -18,6 +18,7 @@ from app.middlewares import require_app_id
|
||||
api = Blueprint("api", __name__)
|
||||
|
||||
|
||||
|
||||
# @api.before_request
|
||||
# def require_api_key_middleware():
|
||||
# """Middleware applied globally to all API routes in this blueprint"""
|
||||
|
||||
Reference in New Issue
Block a user