summaryrefslogtreecommitdiff
path: root/crates/typst-library
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-library
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-library')
-rw-r--r--crates/typst-library/src/compute/data.rs53
-rw-r--r--crates/typst-library/src/meta/bibliography.rs4
-rw-r--r--crates/typst-library/src/text/raw.rs9
3 files changed, 29 insertions, 37 deletions
diff --git a/crates/typst-library/src/compute/data.rs b/crates/typst-library/src/compute/data.rs
index 92c462e6..222b14d3 100644
--- a/crates/typst-library/src/compute/data.rs
+++ b/crates/typst-library/src/compute/data.rs
@@ -1,5 +1,6 @@
use typst::diag::{format_xml_like_error, FileError};
use typst::eval::Bytes;
+use typst::syntax::is_newline;
use crate::prelude::*;
@@ -197,15 +198,16 @@ cast! {
}
/// Format the user-facing CSV error message.
-fn format_csv_error(error: csv::Error, line: usize) -> EcoString {
- match error.kind() {
+fn format_csv_error(err: csv::Error, line: usize) -> EcoString {
+ match err.kind() {
csv::ErrorKind::Utf8 { .. } => "file is not valid utf-8".into(),
csv::ErrorKind::UnequalLengths { expected_len, len, .. } => {
eco_format!(
- "failed to parse csv file: found {len} instead of {expected_len} fields in line {line}"
+ "failed to parse CSV (found {len} instead of \
+ {expected_len} fields in line {line})"
)
}
- _ => "failed to parse csv file".into(),
+ _ => eco_format!("failed to parse CSV ({err})"),
}
}
@@ -278,7 +280,7 @@ pub fn json_decode(
) -> SourceResult<Value> {
let Spanned { v: data, span } = data;
serde_json::from_slice(data.as_slice())
- .map_err(format_json_error)
+ .map_err(|err| eco_format!("failed to parse JSON ({err})"))
.at(span)
}
@@ -302,16 +304,10 @@ pub fn json_encode(
serde_json::to_string(&value)
}
.map(|v| v.into())
- .map_err(|e| eco_format!("failed to encode value as json: {e}"))
+ .map_err(|err| eco_format!("failed to encode value as JSON ({err})"))
.at(span)
}
-/// Format the user-facing JSON error message.
-fn format_json_error(error: serde_json::Error) -> EcoString {
- assert!(error.is_syntax() || error.is_eof());
- eco_format!("failed to parse json file: syntax error in line {}", error.line())
-}
-
/// Reads structured data from a TOML file.
///
/// The file must contain a valid TOML table. TOML tables will be converted into
@@ -366,7 +362,9 @@ pub fn toml_decode(
let raw = std::str::from_utf8(data.as_slice())
.map_err(|_| "file is not valid utf-8")
.at(span)?;
- toml::from_str(raw).map_err(format_toml_error).at(span)
+ toml::from_str(raw)
+ .map_err(|err| format_toml_error(err, raw))
+ .at(span)
}
/// Encodes structured data into a TOML string.
@@ -385,21 +383,21 @@ pub fn toml_encode(
let Spanned { v: value, span } = value;
if pretty { toml::to_string_pretty(&value) } else { toml::to_string(&value) }
.map(|v| v.into())
- .map_err(|e| eco_format!("failed to encode value as toml: {e}"))
+ .map_err(|err| eco_format!("failed to encode value as TOML ({err})"))
.at(span)
}
/// Format the user-facing TOML error message.
-fn format_toml_error(error: toml::de::Error) -> EcoString {
- if let Some(range) = error.span() {
+fn format_toml_error(error: toml::de::Error, raw: &str) -> EcoString {
+ if let Some(head) = error.span().and_then(|range| raw.get(..range.start)) {
+ let line = head.lines().count();
+ let column = 1 + head.chars().rev().take_while(|&c| !is_newline(c)).count();
eco_format!(
- "failed to parse toml file: {}, index {}-{}",
+ "failed to parse TOML ({} at line {line} column {column})",
error.message(),
- range.start,
- range.end
)
} else {
- eco_format!("failed to parse toml file: {}", error.message())
+ eco_format!("failed to parse TOML ({})", error.message())
}
}
@@ -464,7 +462,7 @@ pub fn yaml_decode(
) -> SourceResult<Value> {
let Spanned { v: data, span } = data;
serde_yaml::from_slice(data.as_slice())
- .map_err(format_yaml_error)
+ .map_err(|err| eco_format!("failed to parse YAML ({err})"))
.at(span)
}
@@ -480,15 +478,10 @@ pub fn yaml_encode(
let Spanned { v: value, span } = value;
serde_yaml::to_string(&value)
.map(|v| v.into())
- .map_err(|e| eco_format!("failed to encode value as yaml: {e}"))
+ .map_err(|err| eco_format!("failed to encode value as YAML ({err})"))
.at(span)
}
-/// Format the user-facing YAML error message.
-fn format_yaml_error(error: serde_yaml::Error) -> EcoString {
- eco_format!("failed to parse yaml file: {}", error.to_string().trim())
-}
-
/// Reads structured data from a CBOR file.
///
/// The file must contain a valid cbor serialization. Mappings will be
@@ -529,7 +522,7 @@ pub fn cbor_decode(
) -> SourceResult<Value> {
let Spanned { v: data, span } = data;
ciborium::from_reader(data.as_slice())
- .map_err(|e| eco_format!("failed to parse cbor: {e}"))
+ .map_err(|err| eco_format!("failed to parse CBOR ({err})"))
.at(span)
}
@@ -546,7 +539,7 @@ pub fn cbor_encode(
let mut res = Vec::new();
ciborium::into_writer(&value, &mut res)
.map(|_| res.into())
- .map_err(|e| eco_format!("failed to encode value as cbor: {e}"))
+ .map_err(|err| eco_format!("failed to encode value as CBOR ({err})"))
.at(span)
}
@@ -661,5 +654,5 @@ fn convert_xml(node: roxmltree::Node) -> Value {
/// Format the user-facing XML error message.
fn format_xml_error(error: roxmltree::Error) -> EcoString {
- format_xml_like_error("xml file", error)
+ format_xml_like_error("XML", error)
}
diff --git a/crates/typst-library/src/meta/bibliography.rs b/crates/typst-library/src/meta/bibliography.rs
index 164b1c11..d871db23 100644
--- a/crates/typst-library/src/meta/bibliography.rs
+++ b/crates/typst-library/src/meta/bibliography.rs
@@ -650,8 +650,8 @@ fn parse_bib(path_str: &str, src: &str) -> StrResult<Vec<hayagriva::Entry>> {
}
/// Format a Hayagriva loading error.
-fn format_hayagriva_error(error: YamlBibliographyError) -> EcoString {
- eco_format!("{error}")
+fn format_hayagriva_error(err: YamlBibliographyError) -> EcoString {
+ eco_format!("{err}")
}
/// Format a BibLaTeX loading error.
diff --git a/crates/typst-library/src/text/raw.rs b/crates/typst-library/src/text/raw.rs
index a9d40414..a5699afd 100644
--- a/crates/typst-library/src/text/raw.rs
+++ b/crates/typst-library/src/text/raw.rs
@@ -485,10 +485,9 @@ fn load_syntaxes(paths: &SyntaxPaths, bytes: &[Bytes]) -> StrResult<Arc<SyntaxSe
// We might have multiple sublime-syntax/yaml files
for (path, bytes) in paths.0.iter().zip(bytes.iter()) {
let src = std::str::from_utf8(bytes).map_err(FileError::from)?;
- out.add(
- SyntaxDefinition::load_from_str(src, false, None)
- .map_err(|e| eco_format!("failed to parse syntax file `{path}`: {e}"))?,
- );
+ out.add(SyntaxDefinition::load_from_str(src, false, None).map_err(|err| {
+ eco_format!("failed to parse syntax file `{path}` ({err})")
+ })?);
}
Ok(Arc::new(out.build()))
@@ -528,7 +527,7 @@ fn load_theme(path: EcoString, bytes: Bytes) -> StrResult<Arc<synt::Theme>> {
synt::ThemeSet::load_from_reader(&mut cursor)
.map(Arc::new)
- .map_err(|e| eco_format!("failed to parse theme file `{path}`: {e}"))
+ .map_err(|err| eco_format!("failed to parse theme file `{path}` ({err})"))
}
/// Function to parse the theme argument.