From 562856ba7ff5f3e1908989057f10b7192f5946f7 Mon Sep 17 00:00:00 2001 From: Angel-Dijoux Date: Sun, 12 Nov 2023 00:04:17 +0100 Subject: [PATCH] HOSTFIX: HTTP error. --- src/blueprints/route_handler.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/blueprints/route_handler.py b/src/blueprints/route_handler.py index cc9df3b..47c5b47 100644 --- a/src/blueprints/route_handler.py +++ b/src/blueprints/route_handler.py @@ -1,10 +1,16 @@ +from http.client import HTTPException from typing import Callable from flask import Blueprint, jsonify from flasgger import swag_from from enum import Enum +import jwt from loguru import logger -from src.constants.http_status_codes import HTTP_200_OK, HTTP_500_INTERNAL_SERVER_ERROR +from src.constants.http_status_codes import ( + HTTP_200_OK, + HTTP_401_UNAUTHORIZED, + HTTP_500_INTERNAL_SERVER_ERROR, +) class HttpMethod(Enum): @@ -24,7 +30,16 @@ def decorator(func: Callable) -> Callable: def inner_wrapper(*args, **kwargs): try: return jsonify(func(*args, **kwargs)), HTTP_200_OK + except jwt.ExpiredSignatureError as e: + # Handle JWT token expiration: return 401 Unauthorized + logger.warning(f"Expired JWT token in {func.__name__}: {str(e)}") + return "", HTTP_401_UNAUTHORIZED + except HTTPException as e: + # Handle other HTTP exceptions + logger.warning(f"HTTPException in {func.__name__}: {str(e)}") + return "", e.code except Exception as e: + # Handle generic exceptions logger.warning(f"Error in {func.__name__}: {str(e)}") return "", HTTP_500_INTERNAL_SERVER_ERROR