• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

uc-cdis / fence / 15355556444

30 May 2025 08:46PM UTC coverage: 74.447% (-0.5%) from 74.903%
15355556444

push

github

web-flow
Merge pull request #1233 from uc-cdis/automatedCopy-feature/oidc-oauth-groups-rev2

Automated copy feature/OIDC oauth groups rev2

8175 of 10981 relevant lines covered (74.45%)

0.74 hits per line

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

97.92
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)
1✔
28
    support_email = config.get("SUPPORT_EMAIL_FOR_ERRORS")
1✔
29
    app_name = config.get("APP_NAME", "Gen3 Data Commons")
1✔
30

31
    error_id = _get_error_identifier()
1✔
32
    logger.error(
1✔
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")
1✔
40
    valid_http_status_codes = [
1✔
41
        int(code) for code in list(http_responses.keys()) if int(code) < 500
42
    ]
43

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

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

54
    return (
1✔
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)
1✔
79
    if isinstance(error, APIError):
1✔
80
        if hasattr(error, "json") and error.json:
1✔
81
            error.json["message"] = message
1✔
82
            error_response = error.json, error.code
1✔
83
        else:
84
            error_response = {"message": message}, error.code
1✔
85
    elif isinstance(error, OAuth2Error):
1✔
86
        error_response = {"message": error.description}, error.status_code
1✔
87
    elif isinstance(error, HTTPException):
1✔
88
        error_response = (
1✔
89
            {"message": getattr(error, "description", str(error))},
90
            error.get_response().status_code,
91
        )
92
    else:
93
        logger.exception("Unexpected exception occurred")
1✔
94
        error_code = 500
1✔
95
        if hasattr(error, "code"):
1✔
96
            error_code = error.code
1✔
97
        elif hasattr(error, "status_code"):
1✔
98
            error_code = error.status_code
×
99
        error_response = {"message": message}, error_code
1✔
100

101
    return error_response
1✔
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()
1✔
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