• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

uc-cdis / fence / 16203281716

10 Jul 2025 06:40PM UTC coverage: 28.102% (-46.6%) from 74.666%
16203281716

push

github

paulineribeyre
only run test_login_user_with_idp_already_in_db

3116 of 11088 relevant lines covered (28.1%)

0.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

27.08
fence/error_handler.py
1
import uuid
1✔
2
from http.client import responses as http_responses
1✔
3
from flask import render_template
1✔
4
from werkzeug.exceptions import HTTPException
1✔
5

6
from authlib.oauth2.rfc6749.errors import OAuth2Error
1✔
7
from cdislogging import get_logger
1✔
8

9
from fence.errors import APIError
1✔
10
from fence.config import config
1✔
11
import traceback
1✔
12

13

14
logger = get_logger(__name__)
1✔
15

16

17
def get_error_response(error: Exception):
1✔
18
    """
19
    Generates a response for the given error with detailed logs and appropriate status codes.
20

21
    Args:
22
        error (Exception): The error that occurred.
23

24
    Returns:
25
        Tuple (str, int): Rendered error HTML and HTTP status code.
26
    """
27
    details, status_code = get_error_details_and_status(error)
×
28
    support_email = config.get("SUPPORT_EMAIL_FOR_ERRORS")
×
29
    app_name = config.get("APP_NAME", "Gen3 Data Commons")
×
30

31
    error_id = _get_error_identifier()
×
32
    logger.error(
×
33
        "{} HTTP error occurred. ID: {}\nDetails: {}\nTraceback: {}".format(
34
            status_code, error_id, details, traceback.format_exc()
35
        )
36
    )
37

38
    # Prepare user-facing message
39
    message = details.get("message")
×
40
    valid_http_status_codes = [
×
41
        int(code) for code in list(http_responses.keys()) if int(code) < 500
42
    ]
43

44
    try:
×
45
        status_code = int(status_code)
×
46
        if status_code not in valid_http_status_codes:
×
47
            message = None
×
48
    except (ValueError, TypeError):
×
49
        message = None
×
50
        status_code = 500
×
51

52
    status_code_message = http_responses.get(status_code, "Unknown error code.")
×
53

54
    return (
×
55
        render_template(
56
            "error.html",
57
            app_name=app_name,
58
            status_code=status_code,
59
            status_code_message=status_code_message,
60
            support_email=support_email,
61
            error_id=error_id,
62
            message=message,
63
        ),
64
        status_code,
65
    )
66

67

68
def get_error_details_and_status(error):
1✔
69
    """
70
    Extracts details and HTTP status code from the given error.
71

72
    Args:
73
        error (Exception): The error to process.
74

75
    Returns:
76
        Tuple (dict, int): Error details as a dictionary and HTTP status code.
77
    """
78
    message = error.message if hasattr(error, "message") else str(error)
×
79
    if isinstance(error, APIError):
×
80
        if hasattr(error, "json") and error.json:
×
81
            error.json["message"] = message
×
82
            error_response = error.json, error.code
×
83
        else:
84
            error_response = {"message": message}, error.code
×
85
    elif isinstance(error, OAuth2Error):
×
86
        error_response = {"message": error.description}, error.status_code
×
87
    elif isinstance(error, HTTPException):
×
88
        error_response = (
×
89
            {"message": getattr(error, "description", str(error))},
90
            error.get_response().status_code,
91
        )
92
    else:
93
        logger.exception("Unexpected exception occurred")
×
94
        error_code = 500
×
95
        if hasattr(error, "code"):
×
96
            error_code = error.code
×
97
        elif hasattr(error, "status_code"):
×
98
            error_code = error.status_code
×
99
        error_response = {"message": message}, error_code
×
100

101
    return error_response
×
102

103

104
def _get_error_identifier():
1✔
105
    """
106
    Generates a unique identifier for tracking the error.
107

108
    Returns:
109
        UUID: A unique identifier for the error.
110
    """
111
    return uuid.uuid4()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc