Skip to content

Introduce CustomErrors and DataErrors#554

Open
gtrevisan wants to merge 8 commits intodevfrom
glt/custom-errors
Open

Introduce CustomErrors and DataErrors#554
gtrevisan wants to merge 8 commits intodevfrom
glt/custom-errors

Conversation

@gtrevisan
Copy link
Copy Markdown
Member

  • revamp custom errors with a base CustomError:
    • add a generic DataError class:
      • add subclasses FetchDataError and NanDataError,
    • add a generic CalculationError class:
      • add subclass MismatchCalculationError,
  • have runner.py downgrade all CustomErrors to warnings,
  • have xr.py raise FetchDataErrors rather than catching KeyErrors,
  • add a required=True argument to all data getters to check for all-NaNs arrays,
  • apply a few of these custom exceptions.

Copy link
Copy Markdown
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.

Pull request overview

This PR introduces a structured custom-exception hierarchy for physics-method execution, updates data-connection getters to support required=True (all-NaN detection) and raise data-specific errors, and adjusts runners/physics methods to use and handle these new exceptions more consistently.

Changes:

  • Added CustomError base class with DataError/CalculationError branches and specific subclasses (e.g., FetchDataError, NanDataError, MismatchCalculationError).
  • Updated XarrayDataConnection and MDSConnection getters to accept required and raise FetchDataError/NanDataError where applicable.
  • Updated the physics-method runner to downgrade all CustomErrors to warnings and migrated several call sites to the new exception types.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
disruption_py/machine/mast/util.py Uses MismatchCalculationError for interpolation input-length mismatch.
disruption_py/machine/mast/physics.py Uses MismatchCalculationError for density timebase length mismatch.
disruption_py/machine/d3d/physics.py Raises FetchDataError for an invalid data_source case.
disruption_py/machine/cmod/physics.py Adopts FetchDataError / MismatchCalculationError in a few data/mismatch scenarios and improves one error message.
disruption_py/inout/xr.py Adds required checks and raises FetchDataError/NanDataError instead of returning sentinels.
disruption_py/inout/mds.py Adds required checks and raises NanDataError for all-NaN data.
disruption_py/inout/base.py Extends the DataConnection interface with required: bool = False.
disruption_py/core/physics_method/runner.py Treats any CustomError as warning-level (vs. only CalculationError previously).
disruption_py/core/physics_method/errors.py Defines the new exception hierarchy and subclasses.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread disruption_py/machine/d3d/physics.py
Comment thread disruption_py/machine/cmod/physics.py
Comment on lines +30 to +41


class NanDataError(DataError):
"""
NaN data error.
"""


class MismatchCalculationError(CalculationError):
"""
Mismatch calculation error.
"""
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

The new exception classes don’t provide structured context or helpful default messages. As used elsewhere (e.g., FetchDataError(path) / NanDataError(path)), str(e) becomes only the raw argument, which makes warnings harder to understand. Consider adding __init__ implementations that accept relevant fields (e.g., path, reason, expected/actual) and build a clear message, and optionally store those fields as attributes for downstream handling/logging.

Suggested change
class NanDataError(DataError):
"""
NaN data error.
"""
class MismatchCalculationError(CalculationError):
"""
Mismatch calculation error.
"""
def __init__(self, path, reason=None, message=None):
self.path = path
self.reason = reason
if message is None:
message = f"Failed to fetch data for path {path!r}"
if reason is not None:
message = f"{message}: {reason}"
super().__init__(message)
class NanDataError(DataError):
"""
NaN data error.
"""
def __init__(self, path, reason=None, message=None):
self.path = path
self.reason = reason
if message is None:
message = f"NaN data encountered for path {path!r}"
if reason is not None:
message = f"{message}: {reason}"
super().__init__(message)
class MismatchCalculationError(CalculationError):
"""
Mismatch calculation error.
"""
def __init__(self, expected=None, actual=None, reason=None, message=None):
self.expected = expected
self.actual = actual
self.reason = reason
if message is None:
if expected is not None and actual is not None:
message = (
f"Calculation mismatch: expected {expected!r}, got {actual!r}"
)
elif reason is not None:
message = f"Calculation mismatch: {reason}"
else:
message = "Calculation mismatch"
super().__init__(message)

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

IMHO, overkill. @samc24?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants