parcel_resolver/
error.rs

1use crate::specifier::SpecifierError;
2use crate::PackageJsonError;
3use std::path::PathBuf;
4use std::sync::Arc;
5
6/// An error that occcured during resolution.
7#[derive(Debug, Clone, PartialEq, serde::Serialize)]
8#[serde(tag = "type")]
9pub enum ResolverError {
10  /// An unknown URL scheme was found in the specifier.
11  UnknownScheme { scheme: String },
12  /// An unknown error occurred.
13  UnknownError,
14  /// A file was not found.
15  FileNotFound { relative: PathBuf, from: PathBuf },
16  /// A node_modules directory was not found.
17  ModuleNotFound { module: String },
18  /// A package.json entry field pointed to a non-existent file.
19  ModuleEntryNotFound {
20    /// The node_modules package name.
21    module: String,
22    /// Path of the entry found in package.json.
23    entry_path: PathBuf,
24    /// Path of the package.json.
25    package_path: PathBuf,
26    /// Package.json field name.
27    field: &'static str,
28  },
29  /// A sub-path could not be found within a node_modules package.
30  ModuleSubpathNotFound {
31    /// The node_modules package name.
32    module: String,
33    /// Path of the non-existent file.
34    path: PathBuf,
35    /// Path of the package.json.
36    package_path: PathBuf,
37  },
38  /// An error parsing JSON.
39  JsonError(JsonError),
40  /// An I/O error.
41  IOError(IOError),
42  /// A sub-path was not exported from a package.json.
43  PackageJsonError {
44    /// The node_modules package name.
45    module: String,
46    /// The path of the file that is not exported.
47    path: PathBuf,
48    /// Reason the path was not exported.
49    error: PackageJsonError,
50  },
51  /// A package.json file could not be found above the given path.
52  PackageJsonNotFound { from: PathBuf },
53  /// Could not parse the specifier.
54  InvalidSpecifier(SpecifierError),
55  /// Could not find an extended tsconfig.json file.
56  TsConfigExtendsNotFound {
57    /// Path of the tsconfig.json with the "extends" field.
58    tsconfig: PathBuf,
59    /// Original error resolving the tsconfig.json extends specifier.
60    error: Box<ResolverError>,
61  },
62}
63
64/// An error parsing JSON.
65#[derive(Debug, Clone, PartialEq, serde::Serialize)]
66pub struct JsonError {
67  /// Path of the JSON file.
68  pub path: PathBuf,
69  /// Line number of the error.
70  pub line: usize,
71  /// Column number of the error.
72  pub column: usize,
73  /// Reason for the error.
74  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 {}