Skip to content

Fix RestException crash on null error_code and incorrect except clause#20903

Merged
harupy merged 3 commits intomasterfrom
copilot/fix-restexception-null-error-code
Feb 17, 2026
Merged

Fix RestException crash on null error_code and incorrect except clause#20903
harupy merged 3 commits intomasterfrom
copilot/fix-restexception-null-error-code

Conversation

Copy link
Contributor

Copilot AI commented Feb 17, 2026

Related Issues/PRs

N/A

What changes are proposed in this pull request?

RestException.__init__ crashes with TypeError when the JSON response contains "error_code": null. Two bugs contribute:

  1. Null handling: dict.get("error_code", default) returns None when the key exists with null value, bypassing the default
  2. Exception handling: except ValueError or KeyError: only catches ValueError (Python evaluates or first, returning ValueError instead of a tuple)

Changes:

  • Line 122: json.get("error_code") or ErrorCode.Name(INTERNAL_ERROR) - handles both missing keys and null values
  • Line 136: except (ValueError, KeyError, TypeError): - correctly catches all three exception types
  • Added test cases for null and missing error_code scenarios

Example:

# Previously crashed with TypeError
RestException({"error_code": None, "message": "test"})

# Now defaults to INTERNAL_ERROR
# error_code="INTERNAL_ERROR", message="INTERNAL_ERROR: test"

How is this PR tested?

  • Existing unit/integration tests
  • New unit/integration tests
  • Manual tests

Added test_rest_exception_with_null_error_code and test_rest_exception_with_missing_error_code.

Does this PR require documentation update?

  • No. You can skip the rest of this section.
  • Yes. I've updated:
    • Examples
    • API references
    • Instructions

Does this PR require updating the MLflow Skills repository?

  • No. You can skip the rest of this section.
  • Yes. Please link the corresponding PR or explain how you plan to update it.

Release Notes

Is this a user-facing change?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release notes for MLflow users.

Fixed RestException crash when REST API returns "error_code": null in error responses.

What component(s), interfaces, languages, and integrations does this PR affect?

Components

  • area/tracking: Tracking Service, tracking client APIs, autologging
  • area/models: MLmodel format, model serialization/deserialization, flavors
  • area/model-registry: Model Registry service, APIs, and the fluent client calls for Model Registry
  • area/scoring: MLflow Model server, model deployment tools, Spark UDFs
  • area/evaluation: MLflow model evaluation features, evaluation metrics, and evaluation workflows
  • area/gateway: MLflow AI Gateway client APIs, server, and third-party integrations
  • area/prompts: MLflow prompt engineering features, prompt templates, and prompt management
  • area/tracing: MLflow Tracing features, tracing APIs, and LLM tracing functionality
  • area/projects: MLproject format, project running backends
  • area/uiux: Front-end, user experience, plotting, JavaScript, JavaScript dev server
  • area/build: Build and test infrastructure for MLflow
  • area/docs: MLflow documentation pages

How should the PR be classified in the release notes? Choose one:

  • rn/none - No description will be included. The PR will be mentioned only by the PR number in the "Small Bugfixes and Documentation Updates" section
  • rn/breaking-change - The PR will be mentioned in the "Breaking Changes" section
  • rn/feature - A new user-facing feature worth mentioning in the release notes
  • rn/bug-fix - A user-facing bug fix worth mentioning in the release notes
  • rn/documentation - A user-facing documentation change worth mentioning in the release notes

Should this PR be included in the next patch release?

  • Yes (this PR will be cherry-picked and included in the next patch release)
  • No (this PR will be included in the next minor release)
Original prompt

Fix RestException handling of null error_code

Created by Claude Code for handoff.

Patch release: No

Summary

RestException.__init__ crashes with an unhandled TypeError when the JSON response contains "error_code": null. Two bugs contribute to this:

  1. dict.get("error_code", default) only uses the default when the key is missing, not when the value is None.
  2. The except ValueError or KeyError: clause is a Python bug — it evaluates to except ValueError:, silently ignoring KeyError and TypeError.

Codebase Analysis

  • mlflow/exceptions.py:122 - json.get("error_code", ErrorCode.Name(INTERNAL_ERROR)) does not handle None values
  • mlflow/exceptions.py:136 - except ValueError or KeyError: only catches ValueError due to Python operator precedence (or returns the first truthy operand)
  • mlflow/exceptions.py:128-142 - The fallback chain: tries ErrorCode.Value(), then int() conversion, then logs a warning and defaults to INTERNAL_ERROR

Success Criteria

  • RestException({"error_code": None, "message": "test"}) no longer raises TypeError
  • except clause catches ValueError, KeyError, and TypeError
  • Existing tests pass
  • Add test cases for null/missing error_code

Implementation Notes

  1. Change line 122 from:

    error_code = json.get("error_code", ErrorCode.Name(INTERNAL_ERROR))

    to:

    error_code = json.get("error_code") or ErrorCode.Name(INTERNAL_ERROR)
  2. Change line 136 from:

    except ValueError or KeyError:

    to:

    except (ValueError, KeyError, TypeError):

PR

Title: Fix RestException crash on null error_code and incorrect except clause

Body: Fix two bugs in RestException.__init__: handle null error_code values by using or fallback instead of dict.get default, and fix the except ValueError or KeyError clause to correctly catch both exception types using a tuple.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@harupy harupy marked this pull request as ready for review February 17, 2026 00:38
Copilot AI review requested due to automatic review settings February 17, 2026 00:39
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request.

Co-authored-by: harupy <17039389+harupy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix RestException handling of null error_code Fix RestException crash on null error_code and incorrect except clause Feb 17, 2026
Copilot AI requested a review from harupy February 17, 2026 00:44
@harupy harupy requested a review from TomeHirata February 17, 2026 00:47
@github-actions github-actions bot added v3.10.0 area/tracking Tracking service, tracking client APIs, autologging rn/bug-fix Mention under Bug Fixes in Changelogs. labels Feb 17, 2026
error_code = HTTP_STATUS_TO_ERROR_CODE[int(error_code)]
super().__init__(message, error_code=ErrorCode.Value(error_code))
except ValueError or KeyError:
except (ValueError, KeyError, TypeError):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#20905 to prevent this

Copy link
Collaborator

@TomeHirata TomeHirata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the fix!

@harupy harupy enabled auto-merge February 17, 2026 01:39
@harupy harupy added this pull request to the merge queue Feb 17, 2026
@harupy harupy removed this pull request from the merge queue due to a manual request Feb 17, 2026
Signed-off-by: Harutaka Kawamura <hkawamura0130@gmail.com>
@github-actions
Copy link
Contributor

Documentation preview for 9f56373 is available at:

More info
  • Ignore this comment if this PR does not change the documentation.
  • The preview is updated when a new commit is pushed to this PR.
  • This comment was created by this workflow run.
  • The documentation was built by this workflow run.

@harupy harupy enabled auto-merge February 17, 2026 02:28
@harupy harupy added this pull request to the merge queue Feb 17, 2026
Merged via the queue into master with commit 1824478 Feb 17, 2026
49 checks passed
@harupy harupy deleted the copilot/fix-restexception-null-error-code branch February 17, 2026 02:39
@github-actions github-actions bot added size/S Small PR (10-49 LoC) and removed size/XS labels Feb 17, 2026
daniellok-db pushed a commit to daniellok-db/mlflow that referenced this pull request Feb 20, 2026
…lause (mlflow#20903)

Signed-off-by: Harutaka Kawamura <hkawamura0130@gmail.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: harupy <17039389+harupy@users.noreply.github.com>
Co-authored-by: Harutaka Kawamura <hkawamura0130@gmail.com>
daniellok-db pushed a commit that referenced this pull request Feb 20, 2026
…lause (#20903)

Signed-off-by: Harutaka Kawamura <hkawamura0130@gmail.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: harupy <17039389+harupy@users.noreply.github.com>
Co-authored-by: Harutaka Kawamura <hkawamura0130@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/tracking Tracking service, tracking client APIs, autologging rn/bug-fix Mention under Bug Fixes in Changelogs. size/S Small PR (10-49 LoC) v3.10.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants