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
use futures::channel::mpsc;
use kdl::{KdlDocument, KdlNode};
use miette::Diagnostic;
use thiserror::Error;

use crate::{NpmPackageLock, NpmPackageLockEntry};

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Error, Diagnostic)]
pub enum NodeMaintainerError {
    /// Unsupported resolved URL scheme
    #[error("Unsupported resolved URL scheme")]
    #[diagnostic(code(node_maintainer::kdl::unsupported_url_scheme), url(docsrs))]
    UnsupportedScheme(String),

    /// Failed to parse a resolved URL while parsing lockfile
    #[error("Failed to parse a resolved URL while parsing lockfile: {0}")]
    #[diagnostic(code(node_maintainer::kdl::url_parse_error), url(docsrs))]
    UrlParseError(String, #[source] url::ParseError),

    /// Failed to parse a Semver string.
    #[error("Failed to parse a Semver string.")]
    #[diagnostic(code(node_maintainer::kdl::semver_parse_error), url(docsrs))]
    SemverParseError(#[from] node_semver::SemverError),

    /// Missing version for NPM package entry in lockfile.
    #[error("Missing version for NPM package entry in lockfile.")]
    #[diagnostic(code(node_maintainer::kdl::missing_version), url(docsrs))]
    MissingVersion,

    /// Missing resolution for package entry in lockfile.
    #[error("Missing version for NPM package entry in lockfile.")]
    #[diagnostic(code(node_maintainer::kdl::missing_version), url(docsrs))]
    MissingResolution,

    /// Failed to parse an integrity value.
    #[error(transparent)]
    #[diagnostic(code(node_maintainer::kdl::integrity_parse_error), url(docsrs))]
    IntegrityParseError(#[from] ssri::Error),

    /// Failed to parse an integrity value while loading lockfile.
    #[error("Failed to parse an integrity value while loading lockfile node:\n{0}")]
    #[diagnostic(code(node_maintainer::kdl::integrity_parse_error), url(docsrs))]
    KdlLockfileIntegrityParseError(KdlNode, #[source] ssri::Error),

    /// Missing package node name.
    #[error("Missing package node name:\n{0}")]
    #[diagnostic(code(node_maintainer::kdl::missing_node_name), url(docsrs))]
    KdlLockMissingName(KdlNode),

    /// Missing package node name.
    #[error("Missing package name:\n{0:#?}")]
    #[diagnostic(code(node_maintainer::npm::missing_name), url(docsrs))]
    NpmLockMissingName(Box<NpmPackageLockEntry>),

    /// Failed to parse an integrity value while loading NPM lockfile.
    #[error("Failed to parse an integrity value while loading lockfile node:\n{0:#?}")]
    #[diagnostic(code(node_maintainer::npm::integrity_parse_error), url(docsrs))]
    NpmLockfileIntegrityParseError(Box<NpmPackageLockEntry>, #[source] ssri::Error),

    /// Unsupported NPM Package Lock version.
    #[error("Unsupported NPM Package Lock version: {0}")]
    #[diagnostic(
        code(node_maintainer::npm::unsupported_package_lock_Version),
        url(docsrs)
    )]
    NpmUnsupportedPackageLockVersion(u64),

    /// No root node in KDL lockfile.
    #[error("No root node in KDL lockfile.")]
    #[diagnostic(code(node_maintainer::kdl::missing_root), url(docsrs))]
    KdlLockMissingRoot(KdlDocument),

    /// No root node in NPM lockfile.
    #[error("No root package in NPM lockfile.")]
    #[diagnostic(code(node_maintainer::npm::missing_root), url(docsrs))]
    NpmLockMissingRoot(NpmPackageLock),

    /// Error parsing lockfile.
    #[error(transparent)]
    #[diagnostic(code(node_maintainer::kdl::parse_error), url(docsrs))]
    KdlParseError(#[from] kdl::KdlError),

    #[error("Invalid lockfile version format.")]
    #[diagnostic(code(node_maintainer::kdl::invalid_lockfile_version), url(docsrs))]
    InvalidLockfileVersion,

    /// Error from serde_wasm_bindgen
    #[cfg(target_arch = "wasm32")]
    #[error(transparent)]
    #[diagnostic(code(node_maintainer::serde_wasm_bindgen::error), url(docsrs))]
    SerdeWasmBindgenError(#[from] serde_wasm_bindgen::Error),

    /// Generic package spec error.
    #[error(transparent)]
    #[diagnostic(transparent)]
    PackageSpecError(#[from] oro_package_spec::PackageSpecError),

    /// Generic IO Error.
    #[error("{0}")]
    #[diagnostic(code(node_maintainer::io_error), url(docsrs))]
    IoError(String, #[source] std::io::Error),

    #[cfg(not(target_arch = "wasm32"))]
    /// Generic error returned from Nassun.
    #[error(transparent)]
    #[diagnostic(transparent)]
    NassunError(#[from] nassun::NassunError),

    #[cfg(target_arch = "wasm32")]
    /// Generic error returned from Nassun.
    #[error(transparent)]
    #[diagnostic(transparent)]
    NassunError(#[from] nassun::error::NassunError),

    /// Generic serde_json error.
    #[error(transparent)]
    #[diagnostic(code(node_maintainer::serde_json_error), url(docsrs))]
    SerdeJsonError(#[from] serde_json::Error),

    /// Generic error. Refer to the error message for more details.
    #[error("{0}")]
    #[diagnostic(code(node_maintainer::miscellaneous_error), url(docsrs))]
    MiscError(String),

    /// Failed to send data through mpsc channel. This is likely an internal
    /// error of some sort.
    #[error("Failed to send data through mpsc channel.")]
    #[diagnostic(code(node_maintainer::mpsc_error), url(docsrs))]
    TrySendError,

    /// Failed to validate a graph. Refer to the error message for more details.
    #[error("{0}")]
    #[diagnostic(code(node_maintainer::graph_error), url(docsrs))]
    GraphValidationError(String),

    /// Got an error while walking `node_modules`. Refer to the error message
    /// for specific details.
    #[cfg(not(target_arch = "wasm32"))]
    #[error(transparent)]
    #[diagnostic(code(node_maintainer::walkdir_error), url(docsrs))]
    WalkDirError(#[from] walkdir::Error),

    /// Failed to read `package.json` during the build step. Refer to the
    /// error message for more details.
    #[cfg(not(target_arch = "wasm32"))]
    #[error("Failed to read manifest during build step, at {}", .0.display())]
    #[diagnostic(code(node_maintainer::build_manifest_read_error), url(docsrs))]
    BuildManifestReadError(std::path::PathBuf, #[source] std::io::Error),

    /// Some error occurred while running a script. Refer to the error message
    /// for more details.
    #[cfg(not(target_arch = "wasm32"))]
    #[error(transparent)]
    #[diagnostic(transparent)]
    OroScriptError(#[from] oro_script::OroScriptError),

    /// Locked file was requested, but a new dependency tree was resolved that
    /// would cause changes to the lockfile. The contents of `package.json`
    /// may have changed since the last time the lockfile was updated.
    ///
    /// This typically happens when a dependency is added or removed from
    /// package.json while locked mode is enabled. If you have an existing
    /// lockfile and get this error without any modifications to package.json,
    /// please [report this as a
    /// bug](https://github.com/orogene/orogene/issues/new).
    #[error("Locked file was requested, but a new dependency tree was resolved that would cause changes to the lockfile. The contents of `package.json` may have changed since the last time the lockfile was updated.")]
    #[diagnostic(
        code(node_maintainer::lockfile_mismatch),
        url(docsrs),
        help("Did you modify package.json by hand?")
    )]
    LockfileMismatch,
}

impl<T> From<mpsc::TrySendError<T>> for NodeMaintainerError {
    fn from(_: mpsc::TrySendError<T>) -> Self {
        NodeMaintainerError::TrySendError
    }
}

pub trait IoContext {
    type T;

    fn io_context(self, context: impl FnOnce() -> String) -> Result<Self::T, NodeMaintainerError>;
}

impl<T> IoContext for Result<T, std::io::Error> {
    type T = T;

    fn io_context(self, context: impl FnOnce() -> String) -> Result<Self::T, NodeMaintainerError> {
        self.map_err(|e| NodeMaintainerError::IoError(context(), e))
    }
}