Crates.io | friendly-errors |
lib.rs | friendly-errors |
version | 0.2.0 |
source | src |
created_at | 2022-08-02 04:59:00.454889 |
updated_at | 2022-08-20 03:12:36.125198 |
description | Create developer-friendly error messages |
homepage | |
repository | |
max_upload_size | |
id | 637201 |
size | 38,307 |
Note, this is still a work in progress and is very experimental.
Awesome error messages should be a core part of the experience of using any language. Us programmers hate debugging, so let's make it as easy as possible to debug errors.
Error messages should be helpful. To that end, they should do five things:
This allows users to quickly understand the problem and hopefully fix it in just a few seconds.
Here's the general format for error messages:
--- Error(E1234): name of the error -------------------------------------------
text explaining the error
filepath:line:column
|
72 | function hello_world() {
| ^^^^^^^^^^^
--> try changing "hello_world" to "helloWorld"
Explanation. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
To learn more, read the docs at https://docs.example.com/
Let's break it down.
^
should be used to highlight the exact error location.To get started, just add the following to your Cargo.toml file:
[dependencies]
friendly-errors = "0.1"
Then, you can build up your error message. Every item in this crate uses the builder pattern:
let error_message = FriendlyError::new()
.title("variable is undefined")
.code("E1234")
.add_code_snippet(
FriendlyCodeSnippet::new()
.filepath("src/main.rs")
.contents("let x = foo;")
.line_start(1)
.index_start(8)
.line_end(1)
.index_end(10)
.text("let x = foo;")
.caption("foo is not defined")
)
.add_suggestion("Try defining foo before using it. All variables must be defined before they're used.")
.doc_url("https://github.com/Nick-Mazuk/friendly-errors")
.build() // returns a Result as to provide errors if not all required fields are set
.unwrap();
This will produce the following error message:
--- Error(E1234): variable is undefined ---------------------------------------
src/main.rs:1:8
|
1 | let x = foo;
| ^^^
--> foo is not defined.
Try defining foo before using it. All variables must be defined before they're
used.
To learn more, read the docs at https://github.com/Nick-Mazuk/friendly-errors