Skip to main content

node_auth/
error.rs

1//! Error types for authentication and configuration failures.
2
3use thiserror::Error;
4
5/// Errors that can occur during authentication or configuration file manipulation.
6#[derive(Error, Debug)]
7pub enum AuthError {
8  /// Authentication failure when resolving credentials via ADC or gcloud CLI.
9  #[error("Authentication error: {0}")]
10  Authentication(String),
11
12  /// No eligible registry was discovered in the npm configuration.
13  #[error("Configuration error: No eligible registry configuration found in .npmrc")]
14  NoNpmRegistry,
15
16  /// Neither Application Default Credentials nor gcloud supplied credentials.
17  #[error("Authentication error: Failed to get credentials from ADC or gcloud")]
18  CredentialsUnavailable,
19
20  /// Bun environment credentials conflict with local npm credentials.
21  #[error(
22    "Configuration error: Bun environment credentials cannot be combined with local npm credentials"
23  )]
24  BunEnvLocalCredentialConflict,
25
26  /// Bun environment credentials were requested while Bun support was disabled.
27  #[error("Configuration error: Bun environment credentials require Bun support")]
28  BunEnvDisabledConflict,
29
30  /// File system or I/O failure.
31  #[error("IO error: {0}")]
32  Io(#[from] std::io::Error),
33
34  /// Missing or invalid configuration (e.g. no Artifact Registry scoped registry found).
35  #[error("Configuration error: {0}")]
36  Config(String),
37
38  /// Invalid source or destination Yarn configuration.
39  #[error("YAML parse error in {input}: {source}")]
40  YamlInput {
41    /// Whether the error came from the source or credential configuration.
42    input: &'static str,
43    /// The underlying YAML parse error.
44    #[source]
45    source: yaml_edit::YamlError,
46  },
47
48  /// Invalid Bun configuration.
49  #[error("TOML parse error in bunfig.toml: {0}")]
50  TomlInput(#[from] toml_edit::TomlError),
51
52  /// Regular expression compilation or matching error.
53  #[error("Regex error: {0}")]
54  Regex(#[from] regex::Error),
55}
56
57#[cfg(test)]
58mod tests {
59  use super::*;
60
61  #[test]
62  fn library_errors_do_not_include_cli_guidance() {
63    for error in [
64      AuthError::NoNpmRegistry,
65      AuthError::CredentialsUnavailable,
66      AuthError::BunEnvLocalCredentialConflict,
67      AuthError::BunEnvDisabledConflict,
68      AuthError::Authentication("provider failed".to_string()),
69    ] {
70      let message = error.to_string();
71      assert!(!message.contains('\n'));
72      assert!(!message.contains("--"));
73      assert!(!message.contains("Please run"));
74      assert!(!message.contains("Ensure"));
75    }
76  }
77}