Skip to content

Commit 44a2fad

Browse files
committed
Raise errors instead of panicking
1 parent 7b7f24e commit 44a2fad

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

src/graphql/mod.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,10 @@ impl ScanPaths {
228228

229229
async fn template(&self, name: String) -> async_graphql::Result<String> {
230230
Ok(path_to_string(
231-
self.extra_templates.get(&name).unwrap().render(self),
231+
self.extra_templates
232+
.get(&name)
233+
.ok_or(NoSuchTemplate(name))?
234+
.render(self),
232235
)?)
233236
}
234237

@@ -316,6 +319,10 @@ struct TemplateInput {
316319
template: String,
317320
}
318321

322+
#[derive(Debug, Display, Error)]
323+
#[display("Template {_0:?} not found")]
324+
struct NoSuchTemplate(#[error(ignore)] String);
325+
319326
#[Object]
320327
impl NamedTemplate {
321328
async fn name(&self) -> &str {
@@ -469,7 +476,7 @@ impl Mutation {
469476
.filter(|slct| slct.name() == "template")
470477
.flat_map(|slct| slct.arguments())
471478
.filter_map(|args| {
472-
args.get(0).map(|arg| {
479+
args.first().map(|arg| {
473480
let Value::String(name) = &arg.1 else {
474481
panic!("name isn't a string")
475482
};
@@ -482,12 +489,9 @@ impl Mutation {
482489
.await?
483490
.into_iter()
484491
.map(|template| {
485-
(
486-
template.name,
487-
ScanTemplate::new_checked(&template.template).unwrap(),
488-
)
492+
ScanTemplate::new_checked(&template.template).map(|tmpl| (template.name, tmpl))
489493
})
490-
.collect();
494+
.collect::<Result<_, _>>()?;
491495

492496
Ok(ScanPaths {
493497
directory: DirectoryPath {

0 commit comments

Comments
 (0)