1use crate::specifier::SpecifierError;
2use crate::PackageJsonError;
3use std::path::PathBuf;
4use std::sync::Arc;
5
6#[derive(Debug, Clone, PartialEq, serde::Serialize)]
8#[serde(tag = "type")]
9pub enum ResolverError {
10 UnknownScheme { scheme: String },
12 UnknownError,
14 FileNotFound { relative: PathBuf, from: PathBuf },
16 ModuleNotFound { module: String },
18 ModuleEntryNotFound {
20 module: String,
22 entry_path: PathBuf,
24 package_path: PathBuf,
26 field: &'static str,
28 },
29 ModuleSubpathNotFound {
31 module: String,
33 path: PathBuf,
35 package_path: PathBuf,
37 },
38 JsonError(JsonError),
40 IOError(IOError),
42 PackageJsonError {
44 module: String,
46 path: PathBuf,
48 error: PackageJsonError,
50 },
51 PackageJsonNotFound { from: PathBuf },
53 InvalidSpecifier(SpecifierError),
55 TsConfigExtendsNotFound {
57 tsconfig: PathBuf,
59 error: Box<ResolverError>,
61 },
62}
63
64#[derive(Debug, Clone, PartialEq, serde::Serialize)]
66pub struct JsonError {
67 pub path: PathBuf,
69 pub line: usize,
71 pub column: usize,
73 pub message: String,
75}
76
77impl JsonError {
78 pub fn new(path: PathBuf, err: serde_json::Error) -> JsonError {
79 JsonError {
80 path,
81 line: err.line(),
82 column: err.column(),
83 message: err.to_string(),
84 }
85 }
86}
87
88#[derive(Debug, Clone)]
89pub struct IOError(Arc<std::io::Error>);
90
91impl serde::Serialize for IOError {
92 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
93 where
94 S: serde::Serializer,
95 {
96 #[derive(serde::Serialize)]
97 struct IOErrorMessage {
98 message: String,
99 }
100
101 let msg = IOErrorMessage {
102 message: self.0.to_string(),
103 };
104
105 msg.serialize(serializer)
106 }
107}
108
109impl PartialEq for IOError {
110 fn eq(&self, other: &Self) -> bool {
111 self.0.kind() == other.0.kind()
112 }
113}
114
115impl From<()> for ResolverError {
116 fn from(_: ()) -> Self {
117 ResolverError::UnknownError
118 }
119}
120
121impl From<std::str::Utf8Error> for ResolverError {
122 fn from(_: std::str::Utf8Error) -> Self {
123 ResolverError::UnknownError
124 }
125}
126
127impl From<JsonError> for ResolverError {
128 fn from(e: JsonError) -> Self {
129 ResolverError::JsonError(e)
130 }
131}
132
133impl From<std::io::Error> for ResolverError {
134 fn from(e: std::io::Error) -> Self {
135 ResolverError::IOError(IOError(Arc::new(e)))
136 }
137}
138
139impl From<SpecifierError> for ResolverError {
140 fn from(value: SpecifierError) -> Self {
141 ResolverError::InvalidSpecifier(value)
142 }
143}
144
145impl std::fmt::Display for ResolverError {
146 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
147 write!(f, "{:?}", self)
148 }
149}
150
151impl std::error::Error for ResolverError {}