Skip to content

Introduce KError enum for more maintainable error handling#8

Open
gtrepta wants to merge 3 commits into
masterfrom
errors
Open

Introduce KError enum for more maintainable error handling#8
gtrepta wants to merge 3 commits into
masterfrom
errors

Conversation

@gtrepta
Copy link
Copy Markdown
Contributor

@gtrepta gtrepta commented Feb 19, 2026

No description provided.

@gtrepta gtrepta requested a review from tothtamas28 February 19, 2026 20:36
Copy link
Copy Markdown
Collaborator

@tothtamas28 tothtamas28 left a comment

Choose a reason for hiding this comment

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

Is this change preparing for a specific feature? At the moment, all error types still just wrap a String, but this makes the code more complex and increases coupling between the modules.

Comment thread kframework/src/error.rs
Comment on lines +2 to +5
pub enum KError {
KoreLexerError(String),
KoreParseError(String),
}
Copy link
Copy Markdown
Collaborator

@tothtamas28 tothtamas28 Feb 20, 2026

Choose a reason for hiding this comment

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

Consider defining pub struct KoreLexerError in lexer.rs and pub struct KoreParserError in parser.rs. This reduces coupling between the two modules and keeps error definitions close to their source.

There doesn’t seem to be a strong need to handle these two errors uniformly. But even if such a need exists, you can still define a wrapper type:

pub enum KError {
    ParserError(KoreParserError),
    LexerError(KoreLexerError),
}


impl Id {
pub fn new(s: String) -> Result<Self, String> {
pub fn new(s: String) -> Result<Self, KError> {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

As long as it serves its purpose, I think it's reasonable to keep the error as a String here. Clients of this type can still convert or wrap the error however they see fit.

@gtrepta
Copy link
Copy Markdown
Contributor Author

gtrepta commented Feb 20, 2026

Is this change preparing for a specific feature? At the moment, all error types still just wrap a String, but this makes the code more complex and increases coupling between the modules.

I guess at the moment I'm unsure what the best way to handle errors are. My idea was to make one generic error enum that can be extended with any new variants that are needed in the library, and extending From/Into impls simply adds a match arm.

Your concern about coupling makes sense, though. This would make a lot of modules depend on error.rs. Defining the error structs locally sounds more sensible, and inverses the dependencies if we make a wrapper around all of them (easy to remove it if we want to).

I am trying to create python bindings for the kore structures. The library I am using (pyo3) has a PyErr type which when returned raises an exception on the python side, so I thought it would be a good idea to have some way to control what type of exception is raised (parser/lexer errors can become ValueErrors, for instance). It's likely that I can just continue with Strings and map_err them to ValueErrors for now, so I will shelve this PR at least for today and think about it some more.

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.

2 participants