Skip to content

Implement new error handling pattern for scripting#30

Merged
coal-rock merged 5 commits intomainfrom
cb/talaria-fs-error-handling
May 15, 2025
Merged

Implement new error handling pattern for scripting#30
coal-rock merged 5 commits intomainfrom
cb/talaria-fs-error-handling

Conversation

@coal-rock
Copy link
Owner

New Error Type

Errors now can be described through the following syntax from within Rust:

pub enum ScriptError {
    #[strum(props(class = "fs", name = "OtherFs"), to_string = "{0}")]
    FsError(String),

    #[strum(
        props(class = "fs", name = "FileNotFound"),
        to_string = "could not find file at path: \"{file_path}\""
    )]
    FsFileNotFound { file_path: String },
    ....
  }

Additionally, fs has been migrated to specifically use these errors, and existing errors should continue to function in a second-class manor until manual work is done to port them over.

Use from Rhai

The following getters were created to better take advantage of new context-rich errors:

.class

Returns the class that this error belongs to:

try {
  fs::remove("/"); // rm -rf :P
  sys::reboot();   // it's so joever for them!!!
}
catch (error) {
  switch(error.class) {
    "sys" => print("there was some error relating to the sys module"),
    "fs" => print("there was some error relating to the fs module"),
    _ => print("some other error occurred"),
  }
}

.name

Returns a deterministic string that can be matched upon to identify error type:

try {
   let seed = fs::read("/home/coal/Important/MoneroSeed");
   print("all your monero is mine!!");
   print(seed);
}
catch (error) {
   switch(error.name) {
       "FileNotFound" => print("the monero seed does not exist"),
       "PermissionDenied" => print("no permission to read the monero seed"),
       "IsADirectory" => print("monero seed path is a directory"),
       "InvalidUTF8" => print("monero seed does not contain valid UTF-8"),
       "OtherFs" => print("some other filesystem error occurred:" + error.msg),
       _ => print("some other error occurred"),
   }
}

.msg

Returns message containing human readable description of the error:

try {
   let homework = fs::read("/home/ruby/homework/calc1.mp4");
}
catch (error) {
   print(error.msg); // `file: "/home/ruby/homework/calc1.mp4" contains invalid utf-8`
}

.pretty

Returns a pretty print of the error:

try {
   let homework = fs::read("/home/ruby/homework/calc1.mp4");
}
catch (error) {
   print(error.pretty); // `[fs] InvalidUTF8 - file: "/home/ruby/homework/calc1.mp4" contains invalid utf-8`
}

@coal-rock coal-rock requested a review from engelhartrueben May 15, 2025 04:09
@coal-rock coal-rock added documentation Improvements or additions to documentation scripting Embedded scripting for Pantheon labels May 15, 2025
Copy link
Collaborator

@engelhartrueben engelhartrueben left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

@coal-rock coal-rock merged commit 6b11386 into main May 15, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation scripting Embedded scripting for Pantheon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants