Skip to content

Commit

Permalink
error rework
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Nov 26, 2024
1 parent fb289ce commit 1b1c48b
Show file tree
Hide file tree
Showing 12 changed files with 443 additions and 103 deletions.
452 changes: 387 additions & 65 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ repository = "https://github.com/denoland/deno_doc"
members = ["lib"]

[workspace.dependencies]
deno_graph = { version = "0.85.0", default-features = false, features = ["symbols"] }
deno_ast = { version = "0.43.0" }
import_map = "0.20.0"
deno_graph = { version = "0.85.1", default-features = false, features = ["symbols"] }
deno_ast = { version = "0.44.0" }
thiserror = "2"
deno_error = "0.5.2"
import_map = "0.21.0"
serde = { version = "1.0.204", features = ["derive"] }
indexmap = "2.3.0"

[lib]
crate-type = ["cdylib", "rlib"]
Expand All @@ -25,14 +28,14 @@ name = "ddoc"
required-features = ["comrak"]

[dependencies]
anyhow = "1.0.86"
cfg-if = "1.0.0"
deno_ast.workspace = true
deno_graph.workspace = true
indexmap = "2.3.0"
thiserror.workspace = true
import_map.workspace = true
lazy_static = "1.5.0"
regex = "1.10.6"
indexmap.workspace = true
serde.workspace = true
serde_json = { version = "1.0.122", features = ["preserve_order"] }
termcolor = "1.4.1"
Expand All @@ -55,7 +58,7 @@ pretty_assertions = "1.4.0"
insta = { version = "1.39.0", features = ["json"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
url = "2.4.1"
url = "2.5.3"
percent-encoding = "2.3.1"
wasm-bindgen = "0.2.92"
js-sys = "0.3.69"
Expand All @@ -75,3 +78,6 @@ codegen-units = 1
incremental = true
lto = true
opt-level = "s"

[patch.crates-io]
deno_graph = { path = "../deno_graph" }
4 changes: 2 additions & 2 deletions examples/ddoc/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use deno_doc::DocNodeKind;
use deno_doc::DocParser;
use deno_doc::DocParserOptions;
use deno_doc::DocPrinter;
use deno_graph::source::LoadFuture;
use deno_graph::source::LoadResponse;
use deno_graph::source::Loader;
use deno_graph::source::{LoadError, LoadFuture};
use deno_graph::BuildOptions;
use deno_graph::CapturingModuleAnalyzer;
use deno_graph::GraphKind;
Expand Down Expand Up @@ -43,7 +43,7 @@ impl Loader for SourceFileLoader {
content: content.into(),
})
})
.map_err(|err| err.into())
.map_err(|err| LoadError::Other(std::sync::Arc::new(err)))
} else {
Ok(None)
};
Expand Down
6 changes: 4 additions & 2 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ path = "lib.rs"
crate-type = ["cdylib"]

[dependencies]
anyhow = "1.0.86"
anyhow = "1.0.93"
deno_graph = { workspace = true }
deno_error.workspace = true
deno_doc = { path = "../", default-features = false }
import_map.workspace = true
thiserror.workspace = true
serde.workspace = true
indexmap = "2.6.0"
indexmap.workspace = true

console_error_panic_hook = "0.1.7"
js-sys = "=0.3.69"
Expand Down
32 changes: 23 additions & 9 deletions lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use deno_doc::html::UsageComposerEntry;
use deno_doc::html::UsageToMd;
use deno_doc::DocParser;
use deno_graph::source::CacheSetting;
use deno_graph::source::LoadError;
use deno_graph::source::LoadFuture;
use deno_graph::source::LoadOptions;
use deno_graph::source::LoadResponse;
Expand Down Expand Up @@ -59,6 +60,11 @@ impl Loader for JsLoader {
specifier: &ModuleSpecifier,
options: LoadOptions,
) -> LoadFuture {
#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[class(generic)]
#[error("load rejected or errored")]
struct Reject;

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct JsLoadOptions {
Expand All @@ -83,7 +89,7 @@ impl Loader for JsLoader {
};
response
.map(|value| serde_wasm_bindgen::from_value(value).unwrap())
.map_err(|_| anyhow!("load rejected or errored"))
.map_err(|_| LoadError::Other(std::sync::Arc::new(Reject)))
};
Box::pin(f)
}
Expand All @@ -108,7 +114,7 @@ impl Resolver for ImportMapResolver {
self
.0
.resolve(specifier, &referrer_range.specifier)
.map_err(|err| ResolveError::Other(err.into()))
.map_err(|err| ResolveError::Other(Box::new(err)))
}
}

Expand All @@ -130,19 +136,27 @@ impl Resolver for JsResolver {
referrer_range: &Range,
_mode: deno_graph::source::ResolutionMode,
) -> Result<ModuleSpecifier, ResolveError> {
use ResolveError::*;
#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[class(generic)]
#[error("JavaScript resolve() function threw.")]
struct Reject;

#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[class(generic)]
#[error("{0}")]
struct WasmBindgen(String);

let this = JsValue::null();
let arg0 = JsValue::from(specifier);
let arg1 = JsValue::from(referrer_range.specifier.to_string());
let value = match self.resolve.call2(&this, &arg0, &arg1) {
Ok(value) => value,
Err(_) => {
return Err(Other(anyhow!("JavaScript resolve() function threw.")))
}
Err(_) => return Err(ResolveError::Other(Box::new(Reject))),
};
let value: String = serde_wasm_bindgen::from_value(value)
.map_err(|err| anyhow!("{}", err))?;
ModuleSpecifier::parse(&value).map_err(|err| Other(err.into()))
.map_err(|err| Box::new(WasmBindgen(err.to_string())))?;
ModuleSpecifier::parse(&value)
.map_err(|err| ResolveError::Other(Box::new(err)))
}
}

Expand All @@ -154,7 +168,7 @@ pub async fn doc(
maybe_resolve: Option<js_sys::Function>,
maybe_import_map: Option<String>,
print_import_map_diagnostics: bool,
) -> anyhow::Result<JsValue, JsValue> {
) -> Result<JsValue, JsValue> {
console_error_panic_hook::set_once();
inner_doc(
root_specifier,
Expand Down
3 changes: 1 addition & 2 deletions src/html/jsdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,7 @@ mod test {
)],
),
]),
)
.unwrap();
);

let (a_short_path, nodes) = ctx.doc_nodes.first().unwrap();

Expand Down
12 changes: 6 additions & 6 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const FUSE_FILENAME: &str = "fuse.js";
const SEARCH_JS: &str = include_str!("./templates/pages/search.js");
const SEARCH_FILENAME: &str = "search.js";

fn setup_hbs() -> Result<Handlebars<'static>, anyhow::Error> {
fn setup_hbs() -> Result<Handlebars<'static>, handlebars::TemplateError> {
let mut reg = Handlebars::new();
reg.register_escape_fn(|str| html_escape::encode_safe(str).into_owned());
reg.set_strict_mode(true);
Expand Down Expand Up @@ -282,7 +282,7 @@ impl GenerateCtx {
common_ancestor: Option<PathBuf>,
file_mode: FileMode,
doc_nodes_by_url: IndexMap<ModuleSpecifier, Vec<DocNode>>,
) -> Result<Self, anyhow::Error> {
) -> Self {
let mut main_entrypoint = None;

let doc_nodes = doc_nodes_by_url
Expand Down Expand Up @@ -364,7 +364,7 @@ impl GenerateCtx {
})
.collect::<IndexMap<_, _>>();

Ok(Self {
Self {
package_name: options.package_name,
common_ancestor,
doc_nodes,
Expand All @@ -380,7 +380,7 @@ impl GenerateCtx {
markdown_renderer: options.markdown_renderer,
markdown_stripper: options.markdown_stripper,
head_inject: options.head_inject,
})
}
}

pub fn render<T: serde::Serialize>(
Expand Down Expand Up @@ -797,7 +797,7 @@ pub enum FileMode {
pub fn generate(
mut options: GenerateOptions,
doc_nodes_by_url: IndexMap<ModuleSpecifier, Vec<DocNode>>,
) -> Result<HashMap<String, String>, anyhow::Error> {
) -> Result<HashMap<String, String>, serde_json::Error> {
if doc_nodes_by_url.len() == 1 && options.main_entrypoint.is_none() {
options.main_entrypoint =
Some(doc_nodes_by_url.keys().next().unwrap().clone());
Expand All @@ -817,7 +817,7 @@ pub fn generate(

let common_ancestor = find_common_ancestor(doc_nodes_by_url.keys(), true);
let ctx =
GenerateCtx::new(options, common_ancestor, file_mode, doc_nodes_by_url)?;
GenerateCtx::new(options, common_ancestor, file_mode, doc_nodes_by_url);
let mut files = HashMap::new();

// Index page
Expand Down
3 changes: 1 addition & 2 deletions src/html/render_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,7 @@ mod test {
None,
Default::default(),
doc_nodes_by_url,
)
.unwrap();
);

let (short_path, doc_nodes) = ctx.doc_nodes.first().unwrap();

Expand Down
2 changes: 1 addition & 1 deletion src/html/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub fn generate_search_index(ctx: &GenerateCtx) -> serde_json::Value {

pub(crate) fn get_search_index_file(
ctx: &GenerateCtx,
) -> Result<String, anyhow::Error> {
) -> Result<String, serde_json::Error> {
let search_index = generate_search_index(ctx);
let search_index_str = serde_json::to_string(&search_index)?;

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![deny(clippy::disallowed_types)]
#![deny(clippy::print_stderr)]
#![deny(clippy::print_stdout)]
#![deny(clippy::unnecessary_wraps)]

#[macro_use]
extern crate cfg_if;
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl<'a> DocParser<'a> {
graph: &'a ModuleGraph,
parser: &'a dyn EsParser,
options: DocParserOptions,
) -> Result<Self, anyhow::Error> {
) -> Result<Self, DocError> {
let root_symbol =
Rc::new(deno_graph::symbols::RootSymbol::new(graph, parser));
let visibility = SymbolVisibility::build(graph, &root_symbol)?;
Expand Down
11 changes: 4 additions & 7 deletions tests/html_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Loader for SourceFileLoader {
content: content.into(),
})
})
.map_err(|err| err.into())
.map_err(|err| std::sync::Arc::new(err).into())
} else {
Ok(None)
};
Expand Down Expand Up @@ -366,8 +366,7 @@ async fn symbol_group() {
None,
Default::default(),
doc_nodes_by_url,
)
.unwrap();
);

let mut files = vec![];

Expand Down Expand Up @@ -457,8 +456,7 @@ async fn symbol_search() {
None,
Default::default(),
doc_nodes_by_url,
)
.unwrap();
);

let search_index = generate_search_index(&ctx);

Expand Down Expand Up @@ -505,8 +503,7 @@ async fn module_doc() {
None,
FileMode::Single,
doc_nodes_by_url,
)
.unwrap();
);

let mut module_docs = vec![];

Expand Down

0 comments on commit 1b1c48b

Please sign in to comment.