TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Enhance Rust errors with file and line details

120 点作者 JoelJacobson超过 1 年前

11 条评论

afdbcreid超过 1 年前
I don&#x27;t like having to wrap my function with a macro, with all disadvantages (e.g. worse IDE support), just to get error location. Especially given we can do this without macros at all:<p><pre><code> use std::error::Error as StdError; use std::panic::Location; use std::fmt; #[derive(Debug)] pub struct MyError { pub error: Box&lt;dyn StdError + Send + Sync&gt;, pub location: &amp;&#x27;static Location&lt;&#x27;static&gt;, } impl&lt;E: StdError + Send + Sync + &#x27;static&gt; From&lt;E&gt; for MyError { #[track_caller] #[inline] fn from(error: E) -&gt; Self { Self { error: Box::new(error), location: Location::caller(), } } } impl fmt::Display for MyError { fn fmt(&amp;self, f: &amp;mut fmt::Formatter&lt;&#x27;_&gt;) -&gt; fmt::Result { write!(f, &quot;{}, {}&quot;, self.error, self.location) } } </code></pre> We can also optimize this more (e.g. a simple optimization to be two words instead of three: <a href="https:&#x2F;&#x2F;play.rust-lang.org&#x2F;?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=2a46e8a71103b4a2f432defc320dc06a" rel="nofollow noreferrer">https:&#x2F;&#x2F;play.rust-lang.org&#x2F;?version=stable&amp;mode=debug&amp;editio...</a>. Also allows opting `Send` and `Sync` out or using non-`&#x27;static` lifetimes), but this works as a baseline. The only disadvantage is that if called in a function with `#[track_caller]` this will report the location of the caller... But this is something I can live with.
评论 #37237206 未加载
评论 #37258228 未加载
评论 #37239498 未加载
评论 #37237456 未加载
diarrhea超过 1 年前
Reminds me of `diags` from [0].<p>Side note: can we make &#x27;file not found&#x27; errors that fail to mention <i>what</i> wasn&#x27;t found illegal? What&#x27;s the point of &#x27;file not found&#x27;, then keeping silent about the exact file? Seen it in various ecosystems now.<p>[0]: <a href="https:&#x2F;&#x2F;www.lurklurk.org&#x2F;effective-rust&#x2F;macros.html#when-to-use-macros" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.lurklurk.org&#x2F;effective-rust&#x2F;macros.html#when-to-...</a>
评论 #37233072 未加载
评论 #37233478 未加载
评论 #37233752 未加载
评论 #37233335 未加载
评论 #37232878 未加载
评论 #37233785 未加载
评论 #37233859 未加载
dathinab超过 1 年前
Note that some things, like anyhow do create backtraces (if the feature is enabled) when they are created, including by `?` auto-converting into them and including chaining the backtrace from the error they where created from.<p>Through also note that async code has ... not so nice backtraces, for now, even if you only care about the calling location.
评论 #37233086 未加载
tasn超过 1 年前
We opted for a more manual approach, we have a ctx!() macro[1] we use for wrapping errors we want to enrich that we then use like this[2]: ctx!(some_fallible_fund(foo))?<p>I wodner if anyone is doing anything better? The nice thing about our approach is that we store the data on each error, so we get a full backtrace, even when using async.<p>1: <a href="https:&#x2F;&#x2F;github.com&#x2F;svix&#x2F;svix-webhooks&#x2F;blob&#x2F;main&#x2F;server&#x2F;svix-server&#x2F;src&#x2F;error.rs#L168">https:&#x2F;&#x2F;github.com&#x2F;svix&#x2F;svix-webhooks&#x2F;blob&#x2F;main&#x2F;server&#x2F;svix-...</a><p>2: <a href="https:&#x2F;&#x2F;github.com&#x2F;svix&#x2F;svix-webhooks&#x2F;blob&#x2F;main&#x2F;server&#x2F;svix-server&#x2F;src&#x2F;v1&#x2F;endpoints&#x2F;application.rs#L258">https:&#x2F;&#x2F;github.com&#x2F;svix&#x2F;svix-webhooks&#x2F;blob&#x2F;main&#x2F;server&#x2F;svix-...</a>
评论 #37234360 未加载
JoelJacobson超过 1 年前
Just released `wherr` v0.1.7! Now with optional `anyhow` support via feature flag. This lets you combine `anyhow`&#x27;s error handling with `wherr`&#x27;s file and line details seamlessly. Check out the example in `wherr&#x2F;examples&#x2F;anyhow.rs`.
imetatroll超过 1 年前
There must be some downside to doing this? That is, why wouldn&#x27;t the &#x27;?&#x27; operator already give us this kind of important detail?
评论 #37233013 未加载
评论 #37232825 未加载
dannymi超过 1 年前
Errors are for the regular case when the program detected that something wasn&#x27;t allowed or set up right and the program did everything right (by returning an error). Why then do you need the rs source code file&amp;line of the place where everything went right? What about all the other places where everything went right? Wanna log those, too? :)<p>What will the end user do with rs source code file&amp;line?<p>I read the example on the page. What they want to do is return which argument of `add` the error was with. Well, then introduce an `AddError` (or I guess it could be a more generic `BinaryOperationError`) like this: `struct AddError { argument_1: Option&lt;...&gt;, argument_2: Option&lt;...&gt; }` and return that error from `add`. Then you can even have it return errors due to both arguments at the same time.<p>One use case of the crate would be if you made a mistake and something actually shouldn&#x27;t be an error but should be a panic, and you wanna find where it is in order to change it to a panic. So, debugging.
评论 #37233150 未加载
评论 #37239819 未加载
lewantmontreal超过 1 年前
I’ve always wondered why the backtrace just kind of stops midway to when you finally handle an error, instead of showing up to the `?` where the error happened. I’ve been using anyhow’s `with_context` to add helpful notes to each usage of `?` to get around it.<p>I’ll have to check if this macro is an easier solution!
评论 #37233482 未加载
评论 #37234265 未加载
olih_超过 1 年前
This looks great! Is there a way to enable this automatically for a whole crate, without needing to set it per function?
Animats超过 1 年前
Ah, like automatically adding .context(&quot;msg&quot;) to Return values.
sidit77超过 1 年前
Any reason why you implemented your own proc macro instead of just using std::panic::Location::caller()? Right now I don&#x27;t see the advantage of this crate.
评论 #37234562 未加载