chore: Better error types for TreeBuilder #563
No reviewers
Labels
No labels
Compat/Breaking
Kind
Bad merge
Kind
Bug
Kind
Documentation
Kind
Enhancement
Kind
Feature
Kind
New language
Kind
Security
Kind
Testing
Priority
Critical
Priority
High
Priority
Low
Priority
Medium
Reviewed
Confirmed
Reviewed
Duplicate
Reviewed
Invalid
Reviewed
Won't Fix
Status
Abandoned
Status
Blocked
Status
Need More Info
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: mergiraf/mergiraf#563
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "552-error-types"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This is an attempt to start revamping our error handling (for #552).
I read up a little about the best practices for error handling in Rust, but I'm sure there is still plenty to improve.
It also turns the panic of #556 into a recoverable error, meaning that we fall back on line-based merging in such a case. Similarly, I've changed other explicit panics to recoverable errors. It would still be good to panic on them in development mode and I'm not sure what's the cleanest way to do that. If I add a
panic!
gated by#[config(debug_assertions)]
, rustc complains that the following return statement is unreachable (while it is reachable in release mode). Ideally, the panicking code would not be inserted at every callsite where one such error is raised, but only once, at the definition of the error variant… if it had a sort of constructor, then the panicking could be done there.Since you mentioned it, here's a nice blog post I recently saw on this topic.
yes; for that, you could have a struct for that particular error variant, put the panicking logic into its constructor as you say, and
impl From<ThatErrorStruct> for TreeBuildingError
so that you could do the following in the place you create and then return it with a?
:But I don't really think we'll be throwing the same error from different places all that often.. so I'd go with a
cfg!
-dpanic!
approach, and I think you can avoidunreachable_code
warnings by doing the following:It's of course unfortunate that that'll require us to repeat the error message (once in the
#[error()]
of the enum variant and once in thepanic!
), so maybe we could something like this instead?Not saying that this is pretty of course...
That's indeed one that I enjoyed reading! I didn't go for the recommended solution of having an enum in a struct, because I felt this was mostly motivated by API stability considerations which don't apply to us as we don't intend to maintain a stable Rust API (so far).
Makes sense! I didn't know about
cfg!
, it's a good solution.extra
elements #562oops, apparently forgot to send those out a while ago
@ -69,0 +84,4 @@
NoVirtualRootChildFound,
#[error("impossible to do a line-based fallback merge for a virtual node")]
LineBasedFallbackOnVirtualNode,
#[error("build_conflict returned something else than a conflict")]
build_conflict
lost its surrounding backticks during the move it seems..@ -350,2 +375,4 @@
.next()
.ok_or(TreeBuildingError::NoVirtualRootChildFound),
PCSNode::LeftMarker => {
panic!("impossible to build a subtree for a virtual left marker")
this
panic!
and the next could be turned into an error as well I guess?This comment might've flown under the radar^^
@ -0,0 +1,18 @@
use std::error::Error;
pub(crate) trait InternalError<V, E: Error> {
I think you don't actually need the generics on the trait itself, since you only use them in the impl for
Result
? (the impl can have generics itself)Yes and I forgot to
git add
other changes - let's pretend it's the fault of my train's wifi connection!5c7a18b062
toc407028ca9
I guess we're following the same people on Mastodon:)
Yup, fair enough
chore: Better error types for TreeBuilderto WIP: chore: Better error types for TreeBuilderc407028ca9
tob08cda9a6a
WIP: chore: Better error types for TreeBuilderto chore: Better error types for TreeBuilder@ -0,0 +9,4 @@
/// This is useful for internal errors that are meant to be never reached,
/// but that we want to be able to gracefully recover from in release mode.
#[inline]
fn debug_panic(self) -> Self {
I think this would benefit from
#[track_caller]
, which would make it so that the reported panic location is at the caller of this function and not inside this function@ -279,0 +298,4 @@
_ => {
return Err(TreeBuildingError::MoreThanTwoConflictingSides {
node: predecessor,
});
Correct me if I'm wrong, but isn't this case exactly as unexpected as the "0" case, and if so, shouldn't it be handled in the same way? So if debug assertions are enabled, we would just panic, and otherwise.. I'm not sure whether we should do
commutative_or_line_based_local_fallback
or just return anErr
I couldn't remember it so I just added a
panic!()
there to see when it is reached. Apparently it is reached in delete/delete or delete/modified conflicts, and this fallback to line-based merging is expected, so we shouldn'tdebug_panic
here.Very nice, thank you:)