Skip to content

Feat: Introduce dedicated exception hierarchy in mesa/errors.py #2992

Closed
codebreaker32 wants to merge 7 commits intomesa:mainfrom
codebreaker32:feat/add-errors
Closed

Feat: Introduce dedicated exception hierarchy in mesa/errors.py #2992
codebreaker32 wants to merge 7 commits intomesa:mainfrom
codebreaker32:feat/add-errors

Conversation

@codebreaker32
Copy link
Copy Markdown
Collaborator

This PR introduces a new module, mesa/errors.py, which defines a dedicated and structured exception hierarchy for the Mesa framework. It establishes a base MesaError class and categorizes specific exceptions for Models, Spaces, Scheduling, and Visualization.

Currently, the Mesa codebase relies heavily on generic Python exceptions (such as ValueError, AttributeError, and RuntimeError) to handle failure states.

This creates several issues:

  • Ambiguity: It is difficult for users to distinguish between a Python syntax error and a specific Mesa logic violation.
  • Hard to Catch: Users cannot programmatically catch specific framework errors (e.g., "Agent out of bounds") without writing fragile string-matching logic against ValueError messages.
  • Debugging: Generic errors do not inherently provide context about the Mesa version or the specific subsystem that failed.
  • This PR solves this by providing a standardized set of exceptions that offer semantic meaning to errors.

Implementation

Created mesa/errors.py.

Defined a base class MesaError which automatically prepends the installed Mesa version to all error messages to assist with issue reporting.

Implemented semantic grouping for exceptions:

  1. Model/Config: ConfigurationError, SeedError.
  2. Agent: Agent Related Errors/Exceptions(primaly state errors)
  3. Space: GridDimensionError, OutOfBoundsError, PropertyLayerErrors(a lot of them).
  4. Time: ScheduleError, EventSchedulingError.
  5. Visualization: UserInputError.

Usage Examples

Note: This PR only adds the definitions(to keep it simple for review). In upcoming PRs, I will be updating the codebase will be updated to usage patterns like this:

  1. Improved Error Handling for Users
from mesa.errors import OutOfBoundsError

try:
    grid.move_agent(agent, (999, 999))
except OutOfBoundsError as e:
    # We know exactly what went wrong and can handle it gracefully
    # ....Logic to Handle
  1. Clearer Debugging Output Instead of ValueError( or generic errors)
mesa.errors.GridDimensionError: [Mesa 3.0] Dimensions must be a list of positive integers.

Future Work Plan: I will be opening multiple follow-up PRs to incrementally refactor the codebase to use these new errors.

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.9% [-1.8%, +0.1%] 🔵 +0.2% [+0.1%, +0.3%]
BoltzmannWealth large 🔵 +1.6% [-2.1%, +7.5%] 🔵 -4.3% [-6.2%, -2.7%]
Schelling small 🔵 -0.2% [-1.9%, +1.6%] 🔵 -0.7% [-1.7%, +0.3%]
Schelling large 🔵 +1.0% [-0.3%, +3.6%] 🔵 -0.2% [-1.4%, +0.7%]
WolfSheep small 🔵 +0.1% [-1.1%, +0.9%] 🔵 -0.3% [-0.4%, -0.2%]
WolfSheep large 🔵 -2.0% [-7.2%, +0.8%] 🔵 +1.4% [+0.1%, +2.5%]
BoidFlockers small 🔵 +1.6% [+1.2%, +1.9%] 🔵 +2.5% [+2.3%, +2.7%]
BoidFlockers large 🔵 +1.1% [+0.8%, +1.4%] 🔵 +2.2% [+2.0%, +2.4%]

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

Hi @EwoutH @quaquel
I would love to have your reviews on this

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Dec 23, 2025

Thanks for this. I still need to decide how to proceed with exceptions in MESA. I believe this is a topic that warrants further discussion. For example, there are a bunch of errors in MESA that might also justifiably be subclassed from default Python errors. I don't know what the recommended practice is in Python and would like to read upcommit on this before ting to any specific approach.

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Dec 29, 2025

I have opened a discussion on mesa exceptions in the mesa 4.0 discussion. I am closing this PR, but included a link to it there. Depending on how we decide to move forward, we might reopen this or least draw heavily from it.

@Nithurshen
Copy link
Copy Markdown
Contributor

Nithurshen commented Jan 22, 2026

@codebreaker32, Hello.
I am closely following the discussion on Exceptions for Mesa 4.0. I have experience working on exceptions in other python-based frameworks before. It was me who suggested a exception hierarchy.
I would like to work on the exceptions when a consensus is reached. Is that okay?
Thank you

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

Good to know this, @Nithurshen. You’re welcome to take a stab at it, but I’d suggest first dropping your ideas on #2972 (comment)
The direction isn’t fully crystallized yet (see the comment on #3146), so aligning there first will help us move forward coherently.

@Nithurshen
Copy link
Copy Markdown
Contributor

Good to know this, @Nithurshen. You’re welcome to take a stab at it, but I’d suggest first dropping your ideas on #2972 (comment) The direction isn’t fully crystallized yet (see the comment on #3146), so aligning there first will help us move forward coherently.

It's quite funny. But thank you for your support.
But I'd suggest you to read the whole thread, as I was the one who suggested this idea.

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Jan 22, 2026

I think it’s good to open a separate discussion thread for error types in Mesa. Then we can follow our development process https://github.com/mesa/mesa/blob/main/CONTRIBUTING.md#mesa-development-process

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

My apologies if that came across wrong, @Nithurshen . I wasn't overlooking your previous work on this, I was referring specifically to @quaquel recent note in #3146 about pausing new PRs until we have a consensus.

Since @EwoutH has suggested opening a separate discussion thread, that seems like the best place to finalize the direction. Would be great to see your proposal there :)

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Jan 22, 2026

I'd say there is consensus on the basic idea. Now it's more a matter of fleshing out the details and agreeing on a process. One option is to fully assess the current code and identify all places where a special mesa exception might be useful. The other option is to take a more piecemeal approach. I don't know yet which is better. Also, we might want to establish some basic guidelines for deciding which exception to use.

@Nithurshen
Copy link
Copy Markdown
Contributor

I'd say there is consensus on the basic idea. Now it's more a matter of fleshing out the details and agreeing on a process. One option is to fully assess the current code and identify all places where a special mesa exception might be useful. The other option is to take a more piecemeal approach. I don't know yet which is better. Also, we might want to establish some basic guidelines for deciding which exception to use.

Is it fine if we open a separate discussion thread like @EwoutH suggested?

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Jan 23, 2026

Is it fine if we open a separate discussion thread like @EwoutH suggested?

Of course

@Nithurshen
Copy link
Copy Markdown
Contributor

@EwoutH @quaquel
I have opened a separate discussion thread.

@codebreaker32 codebreaker32 deleted the feat/add-errors branch February 17, 2026 16:46
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.

4 participants