summaryrefslogtreecommitdiff
path: root/crates/typst-cli/src
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-09-11 12:04:37 +0200
committerLaurenz <laurmaedje@gmail.com>2023-09-11 12:04:37 +0200
commit921b40cf9cb75c6412e2421130671b08dcf1ee13 (patch)
tree817735ec1d2dabcbb0cd3d38f73f5e2772eb5300 /crates/typst-cli/src
parent6483d3035bab4df2d644acb738974413977aaa37 (diff)
Forward third-party errors
Better to know something even if it isn't always formatted in the prettiest way
Diffstat (limited to 'crates/typst-cli/src')
-rw-r--r--crates/typst-cli/src/compile.rs14
-rw-r--r--crates/typst-cli/src/main.rs2
-rw-r--r--crates/typst-cli/src/package.rs8
-rw-r--r--crates/typst-cli/src/query.rs4
-rw-r--r--crates/typst-cli/src/tracing.rs4
-rw-r--r--crates/typst-cli/src/update.rs26
-rw-r--r--crates/typst-cli/src/watch.rs7
7 files changed, 36 insertions, 29 deletions
diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs
index c4800171..8cec8953 100644
--- a/crates/typst-cli/src/compile.rs
+++ b/crates/typst-cli/src/compile.rs
@@ -93,7 +93,7 @@ pub fn compile_once(
}
print_diagnostics(world, &[], &warnings, command.common.diagnostic_format)
- .map_err(|_| "failed to print diagnostics")?;
+ .map_err(|err| eco_format!("failed to print diagnostics ({err})"))?;
if let Some(open) = command.open.take() {
open_file(open.as_deref(), &command.output())?;
@@ -115,7 +115,7 @@ pub fn compile_once(
&warnings,
command.common.diagnostic_format,
)
- .map_err(|_| "failed to print diagnostics")?;
+ .map_err(|err| eco_format!("failed to print diagnostics ({err})"))?;
}
}
@@ -135,7 +135,8 @@ fn export(document: &Document, command: &CompileCommand) -> StrResult<()> {
fn export_pdf(document: &Document, command: &CompileCommand) -> StrResult<()> {
let output = command.output();
let buffer = typst::export::pdf(document);
- fs::write(output, buffer).map_err(|_| "failed to write PDF file")?;
+ fs::write(output, buffer)
+ .map_err(|err| eco_format!("failed to write PDF file ({err})"))?;
Ok(())
}
@@ -176,11 +177,14 @@ fn export_image(
ImageExportFormat::Png => {
let pixmap =
typst::export::render(frame, command.ppi / 72.0, Color::WHITE);
- pixmap.save_png(path).map_err(|_| "failed to write PNG file")?;
+ pixmap
+ .save_png(path)
+ .map_err(|err| eco_format!("failed to write PNG file ({err})"))?;
}
ImageExportFormat::Svg => {
let svg = typst::export::svg(frame);
- fs::write(path, svg).map_err(|_| "failed to write SVG file")?;
+ fs::write(path, svg)
+ .map_err(|err| eco_format!("failed to write SVG file ({err})"))?;
}
}
}
diff --git a/crates/typst-cli/src/main.rs b/crates/typst-cli/src/main.rs
index a6e60e5b..c5cfd514 100644
--- a/crates/typst-cli/src/main.rs
+++ b/crates/typst-cli/src/main.rs
@@ -35,7 +35,7 @@ fn main() -> ExitCode {
let _guard = match crate::tracing::setup_tracing(&ARGS) {
Ok(guard) => guard,
Err(err) => {
- eprintln!("failed to initialize tracing {}", err);
+ eprintln!("failed to initialize tracing ({err})");
None
}
};
diff --git a/crates/typst-cli/src/package.rs b/crates/typst-cli/src/package.rs
index bec86516..51a8e4bc 100644
--- a/crates/typst-cli/src/package.rs
+++ b/crates/typst-cli/src/package.rs
@@ -3,6 +3,7 @@ use std::io::{self, Write};
use std::path::{Path, PathBuf};
use codespan_reporting::term::{self, termcolor};
+use ecow::eco_format;
use termcolor::WriteColor;
use typst::diag::{PackageError, PackageResult};
use typst::syntax::PackageSpec;
@@ -50,18 +51,19 @@ fn download_package(spec: &PackageSpec, package_dir: &Path) -> PackageResult<()>
);
print_downloading(spec).unwrap();
+
let data = match download_with_progress(&url) {
Ok(data) => data,
Err(ureq::Error::Status(404, _)) => {
return Err(PackageError::NotFound(spec.clone()))
}
- Err(_) => return Err(PackageError::NetworkFailed),
+ Err(err) => return Err(PackageError::NetworkFailed(Some(eco_format!("{err}")))),
};
let decompressed = flate2::read::GzDecoder::new(data.as_slice());
- tar::Archive::new(decompressed).unpack(package_dir).map_err(|_| {
+ tar::Archive::new(decompressed).unpack(package_dir).map_err(|err| {
fs::remove_dir_all(package_dir).ok();
- PackageError::MalformedArchive
+ PackageError::MalformedArchive(Some(eco_format!("{err}")))
})
}
diff --git a/crates/typst-cli/src/query.rs b/crates/typst-cli/src/query.rs
index 68cf3319..55f04fe8 100644
--- a/crates/typst-cli/src/query.rs
+++ b/crates/typst-cli/src/query.rs
@@ -31,7 +31,7 @@ pub fn query(command: &QueryCommand) -> StrResult<()> {
let serialized = format(data, command)?;
println!("{serialized}");
print_diagnostics(&world, &[], &warnings, command.common.diagnostic_format)
- .map_err(|_| "failed to print diagnostics")?;
+ .map_err(|err| eco_format!("failed to print diagnostics ({err})"))?;
}
// Print diagnostics.
@@ -43,7 +43,7 @@ pub fn query(command: &QueryCommand) -> StrResult<()> {
&warnings,
command.common.diagnostic_format,
)
- .map_err(|_| "failed to print diagnostics")?;
+ .map_err(|err| eco_format!("failed to print diagnostics ({err})"))?;
}
}
diff --git a/crates/typst-cli/src/tracing.rs b/crates/typst-cli/src/tracing.rs
index 80c2ff65..c01efd6d 100644
--- a/crates/typst-cli/src/tracing.rs
+++ b/crates/typst-cli/src/tracing.rs
@@ -128,10 +128,10 @@ impl TracingGuard {
impl Drop for TracingGuard {
fn drop(&mut self) {
if !std::thread::panicking() {
- if let Err(e) = self.finish() {
+ if let Err(err) = self.finish() {
// Since we are finished, we cannot rely on tracing to log the
// error.
- eprintln!("failed to flush tracing flamegraph: {e}");
+ eprintln!("failed to flush tracing flamegraph ({err})");
}
}
}
diff --git a/crates/typst-cli/src/update.rs b/crates/typst-cli/src/update.rs
index 562d7d2d..95418d03 100644
--- a/crates/typst-cli/src/update.rs
+++ b/crates/typst-cli/src/update.rs
@@ -51,15 +51,15 @@ pub fn update(command: &UpdateCommand) -> StrResult<()> {
return self_replace::self_replace(&backup_path)
.and_then(|_| fs::remove_file(&backup_path))
- .map_err(|err| eco_format!("failed to revert to backup: {err}"));
+ .map_err(|err| eco_format!("failed to revert to backup ({err})"));
}
let current_exe = env::current_exe().map_err(|err| {
- eco_format!("failed to locate path of the running executable: {err}")
+ eco_format!("failed to locate path of the running executable ({err})")
})?;
fs::copy(current_exe, &backup_path)
- .map_err(|err| eco_format!("failed to create backup: {err}"))?;
+ .map_err(|err| eco_format!("failed to create backup ({err})"))?;
let release = Release::from_tag(command.version.as_ref())?;
if !update_needed(&release)? && !command.force {
@@ -69,14 +69,14 @@ pub fn update(command: &UpdateCommand) -> StrResult<()> {
let binary_data = release.download_binary(needed_asset()?)?;
let mut temp_exe = NamedTempFile::new()
- .map_err(|err| eco_format!("failed to create temporary file: {err}"))?;
+ .map_err(|err| eco_format!("failed to create temporary file ({err})"))?;
temp_exe
.write_all(&binary_data)
- .map_err(|err| eco_format!("failed to write binary data: {err}"))?;
+ .map_err(|err| eco_format!("failed to write binary data ({err})"))?;
self_replace::self_replace(&temp_exe).map_err(|err| {
fs::remove_file(&temp_exe).ok();
- eco_format!("failed to self-replace running executable: {err}")
+ eco_format!("failed to self-replace running executable ({err})")
})
}
@@ -118,7 +118,7 @@ impl Release {
Err(ureq::Error::Status(404, _)) => {
bail!("release not found (searched at {url})")
}
- Err(_) => bail!("failed to download release (network failed)"),
+ Err(err) => bail!("failed to download release ({err})"),
}
}
@@ -138,7 +138,7 @@ impl Release {
Err(ureq::Error::Status(404, _)) => {
bail!("asset not found (searched for {})", asset.name);
}
- Err(_) => bail!("failed to load asset (network failed)"),
+ Err(err) => bail!("failed to download asset ({err})"),
};
if asset_name.contains("windows") {
@@ -152,7 +152,7 @@ impl Release {
/// Extract the Typst binary from a ZIP archive.
fn extract_binary_from_zip(data: &[u8], asset_name: &str) -> StrResult<Vec<u8>> {
let mut archive = ZipArchive::new(Cursor::new(data))
- .map_err(|err| eco_format!("failed to extract ZIP archive: {err}"))?;
+ .map_err(|err| eco_format!("failed to extract ZIP archive ({err})"))?;
let mut file = archive
.by_name(&format!("{asset_name}/typst.exe"))
@@ -160,7 +160,7 @@ fn extract_binary_from_zip(data: &[u8], asset_name: &str) -> StrResult<Vec<u8>>
let mut buffer = vec![];
file.read_to_end(&mut buffer).map_err(|err| {
- eco_format!("failed to read binary data from ZIP archive: {err}")
+ eco_format!("failed to read binary data from ZIP archive ({err})")
})?;
Ok(buffer)
@@ -172,14 +172,14 @@ fn extract_binary_from_tar_xz(data: &[u8]) -> StrResult<Vec<u8>> {
let mut file = archive
.entries()
- .map_err(|err| eco_format!("failed to extract tar.xz archive: {err}"))?
+ .map_err(|err| eco_format!("failed to extract tar.xz archive ({err})"))?
.filter_map(Result::ok)
.find(|e| e.path().unwrap_or_default().ends_with("typst"))
.ok_or("tar.xz archive did not contain Typst binary")?;
let mut buffer = vec![];
file.read_to_end(&mut buffer).map_err(|err| {
- eco_format!("failed to read binary data from tar.xz archive: {err}")
+ eco_format!("failed to read binary data from tar.xz archive ({err})")
})?;
Ok(buffer)
@@ -235,7 +235,7 @@ fn backup_path() -> StrResult<PathBuf> {
let backup_dir = root_backup_dir.join("typst");
fs::create_dir_all(&backup_dir)
- .map_err(|err| eco_format!("failed to create backup directory: {err}"))?;
+ .map_err(|err| eco_format!("failed to create backup directory ({err})"))?;
Ok(backup_dir.join("typst_backup.part"))
}
diff --git a/crates/typst-cli/src/watch.rs b/crates/typst-cli/src/watch.rs
index b320c651..f53cb5fd 100644
--- a/crates/typst-cli/src/watch.rs
+++ b/crates/typst-cli/src/watch.rs
@@ -25,7 +25,7 @@ pub fn watch(mut command: CompileCommand) -> StrResult<()> {
// Setup file watching.
let (tx, rx) = std::sync::mpsc::channel();
let mut watcher = RecommendedWatcher::new(tx, notify::Config::default())
- .map_err(|_| "failed to setup file watching")?;
+ .map_err(|err| eco_format!("failed to setup file watching ({err})"))?;
// Watch all the files that are used by the input file and its dependencies.
watch_dependencies(&mut world, &mut watcher, HashSet::new())?;
@@ -41,7 +41,8 @@ pub fn watch(mut command: CompileCommand) -> StrResult<()> {
.into_iter()
.chain(std::iter::from_fn(|| rx.recv_timeout(timeout).ok()))
{
- let event = event.map_err(|_| "failed to watch directory")?;
+ let event =
+ event.map_err(|err| eco_format!("failed to watch directory ({err})"))?;
// Workaround for notify-rs' implicit unwatch on remove/rename
// (triggered by some editors when saving files) with the inotify
@@ -94,7 +95,7 @@ fn watch_dependencies(
tracing::info!("Watching {}", path.display());
watcher
.watch(path, RecursiveMode::NonRecursive)
- .map_err(|_| eco_format!("failed to watch {path:?}"))?;
+ .map_err(|err| eco_format!("failed to watch {path:?} ({err})"))?;
}
}