Stack Error Stack Error reduces the up-front cost of designing an error handling solution for your project, so that you focus on writing great libraries and applications. Stack Error has three goals: Provide ergonomics similar to anyhow . Create informative error messages that facilitate debugging. Provide typed data that facilitates runtime error handling. Overview Build informative error messages for debugging with minimal effort. The error message is co-located with the error source, which helps document your code. use stackerror :: prelude :: * ; pub fn process_data ( data : & str ) -> StackResult < String > { let data : Vec < String > = serde_json :: from_str ( data ) . map_err ( stack_map ! ( StackError , "data is not a list of strings" ) ) ? ; data . first ( ) . cloned ( ) . ok_or_else ( stack_else ! ( StackError , "data is empty" ) ) } In this example, [ stack_map! ] and [ stack_err! ] build a new instance of [ StackError ], adding file name and line number information to the message. In the case of [ stack_err! ], the error message stacks onto the existing error. Note that macros are used to simplify common operations, and the same outcome can be achieved using closures instead of macros. If the data isn't a list, the resulting error message would look like: Error: expected value at line 1 column 1 src/process.rs:4 data is not a list of strings The serde error is printed first, followed by the StackError message with file name and line number. Handle errors at runtime by inspecting an optional error code. let data = if data . err_code ( ) == Some ( & ErrorCode :: HttpTooManyRequests ) { // retry } else { data? } ; [ ErrorCode ] includes HTTP error codes, [ std::io::ErrorKind ] codes, and a handful of runtime codes to cover non-HTTP and non-IO cases. You can derive your own error codes as described later in the examples. Define your own error type, allowing you to create custom methods such as [ std::convert::From ] implementations. Provides error types that...
First seen: 2025-05-18 20:52
Last seen: 2025-05-18 23:52