Skip to content

Commit 05d6979

Browse files
committed
Improve propagation of HPS error messages
1 parent fa6742a commit 05d6979

6 files changed

Lines changed: 26 additions & 17 deletions

File tree

crates/hyperpuzzle_impl_nd_euclid/src/hps/puzzle_engine.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl hyperpuzzlescript::EngineCallback<Puzzle> for HpsNdEuclid {
6464
.build_blocking(
6565
&CatalogId::from_str(color_system_id).map_err(|e| eyre!("{e}"))?,
6666
)
67-
.map_err(|e| eyre!("{e:#}")) // cursed reformatting of eyre::Report
67+
.map_err(|e| clone_eyre(&e))
6868
.wrap_err("error building color system")?;
6969
builder.shape().lock().colors = ColorSystemBuilder::unbuild(&colors)?;
7070
}
@@ -77,7 +77,7 @@ impl hyperpuzzlescript::EngineCallback<Puzzle> for HpsNdEuclid {
7777
.build_blocking(
7878
&CatalogId::from_str(twist_system_id).map_err(|e| eyre!("{e}"))?,
7979
)
80-
.map_err(|e| eyre!("{e:#}")) // cursed reformatting of eyre::Report
80+
.map_err(|e| clone_eyre(&e))
8181
.wrap_err("error building twist system")?;
8282
*builder.twists().lock() = TwistSystemBuilder::unbuild(&twists)?;
8383
}
@@ -113,8 +113,9 @@ impl hyperpuzzlescript::EngineCallback<Puzzle> for HpsNdEuclid {
113113
build_fn
114114
.call(build_span, &mut ctx, vec![], Map::new())
115115
.map_err(|e| {
116-
ctx.runtime.report_diagnostic(e);
117-
eyre!("unable to build puzzle `{id}`; see HPS logs")
116+
ctx.runtime
117+
.report_and_convert_to_eyre(e)
118+
.wrap_err("error building puzzle")
118119
})?;
119120

120121
let b = builder.lock();
@@ -128,3 +129,12 @@ impl hyperpuzzlescript::EngineCallback<Puzzle> for HpsNdEuclid {
128129
})
129130
}
130131
}
132+
133+
#[track_caller]
134+
fn clone_eyre(e: &eyre::Report) -> eyre::Report {
135+
if let Some(e) = e.downcast_ref::<FormattedFullDiagnostic>().cloned() {
136+
eyre!(e)
137+
} else {
138+
eyre!("{e}") // cursed reformatting of eyre::Report
139+
}
140+
}

crates/hyperpuzzle_impl_nd_euclid/src/hps/twist_system_engine.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::sync::Arc;
22

3-
use eyre::eyre;
43
use hyperpuzzle_core::prelude::*;
54
use hyperpuzzlescript::*;
65

@@ -55,8 +54,9 @@ impl hyperpuzzlescript::EngineCallback<TwistSystem> for HpsNdEuclid {
5554
let exports = build_fn
5655
.call(build_span, &mut ctx, vec![], Map::new())
5756
.map_err(|e| {
58-
ctx.runtime.report_diagnostic(e);
59-
eyre!("unable to build twist system `{id}`; see HPS logs")
57+
ctx.runtime
58+
.report_and_convert_to_eyre(e)
59+
.wrap_err("error building twist system")
6060
})?;
6161

6262
let mut b = builder.lock();

crates/hyperpuzzlescript/src/builtins/catalog/generators.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl HpsGenerator {
8888
map_fn: impl 'static + Send + Sync + FnOnce(&mut EvalCtx<'_>, Map) -> Result<T>,
8989
) -> eyre::Result<Redirectable<T>> {
9090
// IIFE to mimic try_block
91-
let result = (|| {
91+
(|| {
9292
let mut scope = Scope::default();
9393
scope.special.id = Some(self.generated_id(param_values.clone())?);
9494
let scope = Arc::new(scope);
@@ -142,8 +142,8 @@ impl HpsGenerator {
142142
redirect), list (ID redirect to generator), or map"
143143
.at(ctx.caller_span)),
144144
}
145-
})();
146-
runtime.report_and_convert_to_eyre(result)
145+
})()
146+
.map_err(|e| runtime.report_and_convert_to_eyre(e))
147147
}
148148
}
149149

crates/hyperpuzzlescript/src/diagnostic/full.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl FullDiagnostic {
107107
/// When displayed using [`std::fmt::Display`], only `msg` is shown. Use
108108
/// `.ansi_string` to get the full error with source code snippets and
109109
/// traceback, using ANSI escape codes for coloring.
110-
#[derive(thiserror::Error, Debug)]
110+
#[derive(thiserror::Error, Debug, Clone)]
111111
#[error("{msg}")]
112112
pub struct FormattedFullDiagnostic {
113113
/// Short message describing the error.

crates/hyperpuzzlescript/src/runtime/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,10 @@ impl Runtime {
191191

192192
/// Reports a diagnostic in the case of an error, and converts the error to
193193
/// [`eyre::Report`].
194-
pub fn report_and_convert_to_eyre<T>(&mut self, result: Result<T>) -> eyre::Result<T> {
195-
result.map_err(|e| {
196-
let s = e.formatted(&*self);
197-
self.report_diagnostic(e);
198-
eyre::eyre!("{s}")
199-
})
194+
pub fn report_and_convert_to_eyre(&mut self, e: FullDiagnostic) -> eyre::Report {
195+
let formatted = e.formatted(&*self);
196+
self.report_diagnostic(e);
197+
eyre::eyre!(formatted)
200198
}
201199

202200
/// Locks the map of built-ins and executes a closure with it.

crates/hyperspeedcube/src/gui/tabs/puzzle.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use hyperpuzzle_log::Solve;
1616
use hyperpuzzle_view::{
1717
DragState, HoverMode, NdEuclidViewState, PuzzleSimulation, PuzzleView, PuzzleViewInput,
1818
};
19+
use hyperpuzzlescript::FormattedFullDiagnostic;
1920
use parking_lot::Mutex;
2021

2122
use crate::L;

0 commit comments

Comments
 (0)