Skip to content

Commit

Permalink
fix(prettier): show relevant message when prettier is not installed (t…
Browse files Browse the repository at this point in the history
…ailcallhq#2592)

Co-authored-by: Tushar Mathur <[email protected]>
  • Loading branch information
meskill and tusharmath authored Aug 12, 2024
1 parent e1d7f69 commit 762a66c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tailcall-prettier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
derive_more = { workspace = true }
lazy_static = "1.4.0"
strum_macros = "0.26.2"
thiserror.workspace = true
tokio.workspace = true
58 changes: 30 additions & 28 deletions tailcall-prettier/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,58 @@
use std::fmt::Display;
use std::borrow::Cow;
use std::string::FromUtf8Error;
use std::sync::Arc;

use derive_more::{DebugCustom, From};
use thiserror::Error;
use tokio::task::JoinError;

#[derive(From, DebugCustom)]
#[derive(Error)]
pub enum Error {
#[debug(fmt = "Std IO Error: {}", _0)]
IO(std::io::Error),
#[error("Std IO Error: {0}")]
IO(#[from] std::io::Error),

#[debug(fmt = "Join Error: {}", _0)]
Join(JoinError),
#[error("Join Error: {0}")]
Join(#[from] JoinError),

#[debug(fmt = "From Utf8 Error: {}", _0)]
FromUtf8(FromUtf8Error),
#[error("From Utf8 Error: {0}")]
FromUtf8(#[from] FromUtf8Error),

#[debug(fmt = "Prettier formatting failed: {}", _0)]
#[error("Prettier formatting failed: {0}")]
PrettierFormattingFailed(String),

#[debug(fmt = "No file extension found")]
#[error("{0} command was not found. Ensure you have it installed and available in the PATH")]
CommandNotFound(String),

#[error("No file extension found")]
FileExtensionNotFound,

#[debug(fmt = "Unsupported file type")]
#[error("Unsupported file type")]
UnsupportedFiletype,

#[debug(fmt = "{}\n\nCaused by:\n {}", context, source)]
Context { source: Arc<Error>, context: String },
#[error("{}\n\nCaused by:\n {}", context, source)]
Context {
#[source]
source: Arc<Error>,
context: String,
},
}

impl Display for Error {
impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::IO(error) => write!(f, "Std IO Error: {}", error),
Error::Join(error) => write!(f, "Join Error: {}", error),
Error::FromUtf8(error) => write!(f, "From Utf8 Error: {}", error),
Error::PrettierFormattingFailed(msg) => {
write!(f, "Prettier formatting failed: {}", msg)
}
Error::FileExtensionNotFound => write!(f, "No file extension found"),
Error::UnsupportedFiletype => write!(f, "Unsupported file type"),
Error::Context { source, context } => {
write!(f, "{}\n\nCaused by:\n {}", context, source)
}
}
std::fmt::Display::fmt(self, f)
}
}

impl Error {
pub fn with_context(self, context: String) -> Self {
Error::Context { source: Arc::new(self), context }
}

pub fn from_io_error(command: Cow<'static, str>) -> impl Fn(std::io::Error) -> Self {
move |error| match error.kind() {
std::io::ErrorKind::NotFound => Error::CommandNotFound(command.to_string()),
_ => Error::IO(error),
}
}
}

pub type Result<A> = std::result::Result<A, Error>;
6 changes: 5 additions & 1 deletion tailcall-prettier/src/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ impl Prettier {
child = child.arg("--config").arg(config);
}

let mut child = child.stdin(Stdio::piped()).stdout(Stdio::piped()).spawn()?;
let mut child = child
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.map_err(Error::from_io_error("prettier".into()))?;

if let Some(ref mut stdin) = child.stdin {
stdin.write_all(source.as_bytes())?;
Expand Down

0 comments on commit 762a66c

Please sign in to comment.