From 203a211ee248dae146bfb2bf5e8e02c2aca8d181 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 21 Jan 2023 21:25:42 +0100 Subject: [PATCH] Improve mapping error handling --- src/mapper/error.rs | 10 +++++++++- src/mapper/mapped_dir.rs | 18 +++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/mapper/error.rs b/src/mapper/error.rs index a712ee1..dfcbabf 100644 --- a/src/mapper/error.rs +++ b/src/mapper/error.rs @@ -1,4 +1,4 @@ -use std::io; +use std::{io, path::PathBuf}; use miette::Diagnostic; use thiserror::Error; @@ -24,4 +24,12 @@ pub enum MapperError { #[error("IO Error: {0}")] Io(#[from] io::Error), + + #[error("Failed to map directory {src:?}")] + DirMapping { + src: PathBuf, + + #[source] + err: io::Error, + }, } diff --git a/src/mapper/mapped_dir.rs b/src/mapper/mapped_dir.rs index 7775575..e0b48b0 100644 --- a/src/mapper/mapped_dir.rs +++ b/src/mapper/mapped_dir.rs @@ -1,5 +1,6 @@ use std::{ collections::HashSet, + io, path::{Path, PathBuf}, }; @@ -7,7 +8,7 @@ use tokio::fs::{self, DirEntry}; use crate::{consts::BIN_DIR, repository::node_path::NodePath}; -use super::error::MapperResult; +use super::error::{MapperError, MapperResult}; struct NodeApp { info: DirEntry, @@ -30,11 +31,13 @@ impl NodeApp { /// creates wrappers to map this application pub async fn map_executable(&self) -> MapperResult<()> { let src_path = BIN_DIR.join(self.info.file_name()); - self.write_wrapper_script(&src_path).await + self.write_wrapper_script(&src_path) + .await + .map_err(|err| MapperError::DirMapping { src: src_path, err }) } #[cfg(not(target_os = "windows"))] - async fn write_wrapper_script(&self, path: &Path) -> MapperResult<()> { + async fn write_wrapper_script(&self, path: &Path) -> Result<(), io::Error> { fs::write(path, format!("#!/bin/sh\nnenv exec {} \"$@\"", self.name)).await?; let src_metadata = self.info.metadata().await?; fs::set_permissions(&path, src_metadata.permissions()).await?; @@ -43,7 +46,7 @@ impl NodeApp { } #[cfg(target_os = "windows")] - async fn write_wrapper_script(&self, path: &Path) -> MapperResult<()> { + async fn write_wrapper_script(&self, path: &Path) -> Result<(), io::Error> { fs::write( path.with_extension("bat"), format!("nenv exec {} %*", self.name), @@ -74,7 +77,12 @@ pub async fn map_node_bin(node_path: NodePath) -> MapperResult<()> { async fn get_applications(path: &PathBuf) -> MapperResult> { let mut files = Vec::new(); - let mut iter = fs::read_dir(path).await?; + let mut iter = fs::read_dir(path) + .await + .map_err(|err| MapperError::DirMapping { + src: path.to_owned(), + err, + })?; while let Some(entry) = iter.next_entry().await? { if entry.path().is_file() {