Skip to content

[Detail Bug] monitoring_query returns 500 instead of 401 on auth failures (uncaught ServerError)Β #293

@detail-app

Description

@detail-app

Detail Bug Report

https://app.detail.dev/org_4c5b920c-9e04-4530-a4d2-953927859978/bugs/bug_68ad555f-f9c9-4d88-aeb7-9509bb71238f

Summary

  • Context: The monitoring_query function handles GET requests to /monitoring/queries/{sfqid} endpoint, which is used by the Snowflake Python connector to check query status for async queries.
  • Bug: The monitoring_query function does not handle ServerError exceptions that can be raised by to_token() and to_conn().
  • Actual vs. expected: When authentication fails (missing or invalid token), the function returns a 500 Internal Server Error instead of a 401 Unauthorized error with proper JSON error response.
  • Impact: Clients receive generic 500 errors instead of proper 401 authentication errors, making it difficult to diagnose authentication issues and potentially causing the Snowflake connector's error handling to fail unexpectedly.

Code with bug

def monitoring_query(request: Request) -> JSONResponse:
    token = to_token(request)  # <-- BUG πŸ”΄ Can raise ServerError, but not caught
    conn = to_conn(token)

    sfqid = request.path_params["sfqid"]
    if not conn.results_cache.get(sfqid):
        return JSONResponse({"data": {"queries": []}, "success": True})

    return JSONResponse({"data": {"queries": [{"status": "SUCCESS"}]}, "success": True})

Example

  • Without Authorization header:
    • to_token(request) raises ServerError(status_code=401, code="390101", message="Authorization header not found in the request data.").
    • Unhandled in monitoring_query, Starlette returns 500 Internal Server Error to the client.
  • With invalid token:
    • to_conn(token) raises ServerError(status_code=401, code="390104", message="User must login again to access the service.").
    • Unhandled in monitoring_query, resulting in 500 Internal Server Error.

Recommended fix

Wrap the calls in a try-except and return a JSON error response consistent with other endpoints:

def monitoring_query(request: Request) -> JSONResponse:
    try:  # <-- FIX 🟒
        token = to_token(request)
        conn = to_conn(token)

        sfqid = request.path_params["sfqid"]
        if not conn.results_cache.get(sfqid):
            return JSONResponse({"data": {"queries": []}, "success": True})

        return JSONResponse({"data": {"queries": [{"status": "SUCCESS"}]}, "success": True})

    except ServerError as e:  # <-- FIX 🟒
        return JSONResponse(
            {"data": None, "code": e.code, "message": e.message, "success": False, "headers": None},
            status_code=e.status_code,
        )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions