1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use std::sync::LazyLock;
use std::{fmt::Display, sync::Arc};

use miette::{Diagnostic, LabeledSpan, MietteDiagnostic, Severity, SourceCode, SourceSpan};
use swc_core::common::SourceFile;
use thiserror::Error;

use crate::RspackSeverity;

#[allow(clippy::rc_buffer)]
static EMPTY_STRING: LazyLock<Arc<String>> = LazyLock::new(|| Arc::new("".to_string()));

#[derive(Debug, Error)]
#[error(transparent)]
pub struct InternalError(#[from] Box<dyn Diagnostic + Send + Sync + 'static>);

impl InternalError {
  pub fn new(message: String, severity: RspackSeverity) -> Self {
    Self(Box::new(
      MietteDiagnostic::new(message).with_severity(severity.into()),
    ))
  }
}

/// Convenience [`Diagnostic`] that can be used as an "anonymous" wrapper for
/// Errors. This is intended to be paired with [`IntoDiagnostic`].
#[derive(Debug, Error)]
#[error(transparent)]
pub struct DiagnosticError(Box<dyn std::error::Error + Send + Sync + 'static>);
impl Diagnostic for DiagnosticError {}

impl From<Box<dyn std::error::Error + Send + Sync + 'static>> for DiagnosticError {
  fn from(value: Box<dyn std::error::Error + Send + Sync + 'static>) -> Self {
    Self(value)
  }
}

/// Handle [anyhow::Error]
/// Please try NOT to use this as much as possible.
#[derive(Debug, Error, Diagnostic)]
#[error(transparent)]
pub struct AnyhowError(#[from] anyhow::Error);

/// ## Warning
/// For a [TraceableError], the path is required.
/// Because if the source code is missing when you construct a [TraceableError], we could read it from file system later
/// when convert it into [crate::Diagnostic], but the reverse will not working.
#[derive(Debug, Clone, Error)]
#[error("{title}: {message}")]
pub struct TraceableError {
  title: String,
  kind: DiagnosticKind,
  message: String,
  severity: Severity,
  #[allow(clippy::rc_buffer)]
  src: Arc<String>,
  label: SourceSpan,
  help: Option<String>,
  url: Option<String>,
  hide_stack: Option<bool>,
}

impl Diagnostic for TraceableError {
  fn severity(&self) -> Option<Severity> {
    Some(self.severity)
  }

  fn help(&self) -> Option<Box<dyn Display + '_>> {
    self
      .help
      .as_ref()
      .map(Box::new)
      .map(|c| c as Box<dyn Display>)
  }

  fn url(&self) -> Option<Box<dyn Display + '_>> {
    self
      .url
      .as_ref()
      .map(Box::new)
      .map(|c| c as Box<dyn Display>)
  }

  fn source_code(&self) -> Option<&dyn SourceCode> {
    Some(&self.src)
  }

  fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
    use miette::macro_helpers::{OptionalWrapper, ToOption};
    std::option::Option::Some(Box::new(
      vec![OptionalWrapper::<SourceSpan>::new()
        .to_option(&self.label)
        .map(|label| miette::LabeledSpan::new_with_span(None, *label))]
      .into_iter()
      .filter(Option::is_some)
      .flatten(),
    ))
  }
}

impl TraceableError {
  pub fn with_severity(mut self, severity: impl Into<Severity>) -> Self {
    self.severity = severity.into();
    self
  }

  pub fn with_kind(mut self, kind: DiagnosticKind) -> Self {
    self.kind = kind;
    self
  }

  pub fn with_help(mut self, help: Option<impl Into<String>>) -> Self {
    self.help = help.map(|h| h.into());
    self
  }

  pub fn with_url(mut self, url: Option<impl Into<String>>) -> Self {
    self.url = url.map(|u| u.into());
    self
  }

  pub fn with_hide_stack(mut self, hide_stack: Option<bool>) -> Self {
    self.hide_stack = hide_stack;
    self
  }

  pub fn from_source_file(
    source_file: &SourceFile,
    start: usize,
    end: usize,
    title: String,
    message: String,
  ) -> Self {
    Self::from_arc_string(source_file.src.clone(), start, end, title, message)
  }

  pub fn from_file(
    file_src: String,
    start: usize,
    end: usize,
    title: String,
    message: String,
  ) -> Self {
    Self::from_arc_string(Arc::new(file_src), start, end, title, message)
  }

  pub fn from_empty_file(start: usize, end: usize, title: String, message: String) -> Self {
    Self::from_arc_string(EMPTY_STRING.clone(), start, end, title, message)
  }

  pub fn from_arc_string(
    src: Arc<String>,
    start: usize,
    end: usize,
    title: String,
    message: String,
  ) -> Self {
    Self {
      title,
      kind: Default::default(),
      message,
      severity: Default::default(),
      src,
      label: SourceSpan::new(start.into(), end.saturating_sub(start).into()),
      help: None,
      url: None,
      hide_stack: None,
    }
  }

  pub fn hide_stack(&self) -> Option<bool> {
    self.hide_stack
  }
}

/// Multiple errors to represent different kinds of errors.
/// NEVER implement this with [miette::Diagnostic],
/// because it makes code hard to maintain.
#[derive(Debug, Default)]
pub struct BatchErrors(pub Vec<miette::Error>);

impl BatchErrors {
  pub fn into_inner(self) -> Vec<miette::Error> {
    self.0
  }
}

impl From<BatchErrors> for Vec<crate::Diagnostic> {
  fn from(value: BatchErrors) -> Self {
    value.0.into_iter().map(crate::Diagnostic::from).collect()
  }
}

impl From<miette::Error> for BatchErrors {
  fn from(value: miette::Error) -> Self {
    Self(vec![value])
  }
}

impl From<Vec<miette::Error>> for BatchErrors {
  fn from(value: Vec<miette::Error>) -> Self {
    Self(value)
  }
}

#[macro_export]
macro_rules! impl_diagnostic_transparent {
  (code = $value:expr, $ty:ty) => {
    impl miette::Diagnostic for $ty {
      fn code<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
        Some(Box::new($value))
      }

      fn severity(&self) -> Option<miette::Severity> {
        self.0.severity()
      }

      fn help<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
        self.0.help()
      }

      fn url<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
        self.0.url()
      }

      fn source_code(&self) -> Option<&dyn miette::SourceCode> {
        self.0.source_code()
      }

      fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
        self.0.labels()
      }

      fn related<'a>(
        &'a self,
      ) -> Option<Box<dyn Iterator<Item = &'a dyn miette::Diagnostic> + 'a>> {
        self.0.related()
      }

      fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
        self.0.diagnostic_source()
      }
    }
  };
  ($ty:ty) => {
    impl miette::Diagnostic for $ty {
      fn code<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
        self.0.code()
      }

      fn severity(&self) -> Option<miette::Severity> {
        self.0.severity()
      }

      fn help<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
        self.0.help()
      }

      fn url<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
        self.0.url()
      }

      fn source_code(&self) -> Option<&dyn miette::SourceCode> {
        self.0.source_code()
      }

      fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
        self.0.labels()
      }

      fn related<'a>(
        &'a self,
      ) -> Option<Box<dyn Iterator<Item = &'a dyn miette::Diagnostic> + 'a>> {
        self.0.related()
      }

      fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
        self.0.diagnostic_source()
      }
    }
  };
}

impl_diagnostic_transparent!(InternalError);

#[macro_export]
macro_rules! impl_error_transparent {
  ($ty:ty) => {
    impl std::error::Error for ModuleBuildError {
      fn source(&self) -> ::core::option::Option<&(dyn std::error::Error + 'static)> {
        std::error::Error::source(<Error as AsRef<dyn std::error::Error>>::as_ref(&self.0))
      }
    }

    #[allow(unused_qualifications)]
    impl ::core::fmt::Display for ModuleBuildError {
      #[allow(clippy::used_underscore_binding)]
      fn fmt(&self, __formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Display::fmt(&self.0, __formatter)
      }
    }
  };
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum DiagnosticKind {
  JavaScript,
  Typescript,
  Jsx,
  Tsx,
  Scss,
  Css,
  #[default]
  Internal,
  Io,
  Json,
  Html,
}

/// About the manually implementation,
/// display string should be snake, for consistency.
impl std::fmt::Display for DiagnosticKind {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      DiagnosticKind::JavaScript => write!(f, "javascript"),
      DiagnosticKind::Typescript => write!(f, "typescript"),
      DiagnosticKind::Jsx => write!(f, "jsx"),
      DiagnosticKind::Tsx => write!(f, "tsx"),
      DiagnosticKind::Scss => write!(f, "scss"),
      DiagnosticKind::Css => write!(f, "css"),
      DiagnosticKind::Internal => write!(f, "internal"),
      DiagnosticKind::Io => write!(f, "io"),
      DiagnosticKind::Json => write!(f, "json"),
      DiagnosticKind::Html => write!(f, "html"),
    }
  }
}

fn _assert() {
  fn _assert_send_sync<T: Send + Sync>() {}
  _assert_send_sync::<InternalError>();
  _assert_send_sync::<DiagnosticError>();
}

/// (message, stack, backtrace, hide_stack)
#[derive(Debug, Error, Diagnostic)]
#[diagnostic()]
#[error("{reason}\n{backtrace}")]
pub struct NodeError {
  pub reason: String,
  pub stack: Option<String>,
  pub backtrace: String,
  pub hide_stack: Option<bool>,
}