1use std::path::PathBuf;
4
5use thiserror::Error;
6
7use super::push_plan::PushPlanError;
8use crate::error::{AuthFailure, RemoteUnreadableReason};
9use crate::fetch_request::FetchRefspecError;
10use crate::partial_clone::PartialCloneError;
11use crate::protocol::ProtocolError;
12use crate::resumable::ResumableError;
13use crate::serve::ServeError;
14
15#[non_exhaustive]
17#[derive(Debug, Error)]
18pub enum RemoteError {
19 #[error("{}", match remote {
21 Some(name) => format!("remote '{name}' has no URL configured"),
22 None => "remote URL is empty".to_owned(),
23 })]
24 NoUrl {
25 remote: Option<String>,
27 },
28 #[error("unsupported remote URL scheme")]
30 UnsupportedScheme,
31 #[error("{detail}")]
33 InvalidUrl {
34 detail: String,
36 },
37 #[error("HTTP remotes require the http-ureq feature or an HttpClientFactory")]
39 HttpUnavailable,
40 #[error("unknown remote '{name}'")]
42 UnknownRemote {
43 name: String,
45 },
46 #[error("remote '{name}' already exists")]
49 RemoteExists {
50 name: String,
52 },
53 #[error("'{name}' is not a valid remote name")]
55 InvalidRemoteName {
56 name: String,
58 },
59
60 #[error("authentication failed for {url}: {reason}")]
62 Auth {
63 url: String,
65 reason: AuthFailure,
67 },
68 #[error("{}", crate::credentials::NON_INTERACTIVE_MESSAGE)]
70 CredentialsUnavailable {
71 helpers: Vec<String>,
73 },
74 #[error("the remote does not support atomic pushes")]
76 PushAtomicUnsupported,
77 #[error("the receiving end does not support push options")]
79 PushOptionsUnsupported,
80 #[error("repository is not shallow")]
82 FetchUnshallowNotShallow,
83 #[error("the remote does not support shallow fetches")]
85 FetchShallowUnsupported,
86 #[error("the remote does not support deepen-relative")]
88 FetchDeepenRelativeUnsupported,
89 #[error(
91 "received pack file missing: {}. If this persists, check real-time antivirus or disk cleanup for quarantined pack temp files.",
92 crate::git_path::user_display_path(.path)
93 )]
94 ReceivedPackTempMissing {
95 path: PathBuf,
97 },
98 #[error("{}", crate::error::remote_unreadable_message(url.as_deref(), reason))]
100 Unreadable {
101 url: Option<String>,
103 reason: RemoteUnreadableReason,
105 },
106 #[error("remote error: {message}")]
108 Fatal {
109 message: String,
111 },
112 #[error("local upload-pack: {0}")]
114 LocalUploadPack(Box<ServeError>),
115 #[error(transparent)]
117 Serve(ServeError),
118 #[error("local upload-pack stopped unexpectedly")]
120 LocalUploadPackPanicked,
121 #[error(transparent)]
123 Resumable(#[from] Box<ResumableError>),
124 #[error("{0}")]
126 FetchRefspec(#[from] FetchRefspecError),
127 #[error("cannot fetch into checked-out branch '{branch}'")]
129 FetchIntoCheckedOutBranch {
130 branch: String,
132 },
133 #[error(transparent)]
135 Protocol(#[from] ProtocolError),
136 #[error(transparent)]
138 PushPlan(#[from] PushPlanError),
139 #[error(transparent)]
141 PartialClone(#[from] PartialCloneError),
142 #[error("{detail}")]
144 MalformedTransportUrl {
145 scheme: &'static str,
147 detail: String,
149 },
150 #[error("{detail}")]
152 Ssh {
153 detail: String,
155 },
156 #[error("{}", crate::error::remote_transport_message(.url.as_deref(), .detail))]
158 Transport {
159 url: Option<String>,
161 detail: String,
163 },
164 #[error("{}", crate::error::remote_http_status_message(.url.as_deref(), *status))]
166 HttpStatus {
167 url: Option<String>,
169 status: u16,
171 },
172 #[error("{detail}")]
174 Http {
175 detail: String,
177 },
178 #[error("HTTP worker thread panicked")]
180 HttpWorkerPanicked,
181 #[error("{detail}")]
183 HttpLowSpeedStall {
184 detail: String,
186 },
187 #[error("{detail}")]
189 Credential {
190 detail: String,
192 },
193 #[error(transparent)]
195 Snapshot(crate::snapshot_checkout::CheckoutError),
196 #[error(transparent)]
198 Clone(Box<crate::clone::CloneError>),
199 #[error(transparent)]
201 Bundle(crate::bundle::BundleError),
202}
203
204impl RemoteError {
205 pub fn from_io(err: std::io::Error) -> Self {
207 Self::Transport {
208 url: None,
209 detail: err.to_string(),
210 }
211 }
212
213 #[must_use]
215 pub fn is_remote_unreadable(&self) -> bool {
216 matches!(self, Self::Unreadable { .. })
217 }
218
219 #[must_use]
221 pub fn kind_name(&self) -> &'static str {
222 match self {
223 Self::NoUrl { .. } => "remote_no_url",
224 Self::UnsupportedScheme => "unsupported_scheme",
225 Self::InvalidUrl { .. } => "invalid_url",
226 Self::HttpUnavailable => "http_unavailable",
227 Self::UnknownRemote { .. } => "unknown_remote",
228 Self::RemoteExists { .. } => "remote_exists",
229 Self::InvalidRemoteName { .. } => "invalid_remote_name",
230 Self::Auth { .. } => "auth",
231 Self::CredentialsUnavailable { .. } => "credentials_unavailable",
232 Self::PushAtomicUnsupported => "push_atomic_unsupported",
233 Self::PushOptionsUnsupported => "push_options_unsupported",
234 Self::FetchUnshallowNotShallow => "fetch_unshallow_not_shallow",
235 Self::FetchShallowUnsupported => "fetch_shallow_unsupported",
236 Self::FetchDeepenRelativeUnsupported => "fetch_deepen_relative_unsupported",
237 Self::ReceivedPackTempMissing { .. } => "received_pack_temp_missing",
238 Self::Unreadable { .. } => "remote_unreadable",
239 Self::Fatal { .. } => "remote_fatal",
240 Self::LocalUploadPack(_) => "local_upload_pack",
241 Self::Serve(_) => "serve",
242 Self::LocalUploadPackPanicked => "local_upload_pack_panicked",
243 Self::Resumable(_) => "resumable",
244 Self::FetchRefspec(_) => "fetch_refspec",
245 Self::FetchIntoCheckedOutBranch { .. } => "fetch_into_checked_out_branch",
246 Self::Protocol(_) => "protocol",
247 Self::PushPlan(_) => "push_plan",
248 Self::PartialClone(_) => "partial_clone",
249 Self::MalformedTransportUrl { .. } => "malformed_transport_url",
250 Self::Transport { .. } => "transport",
251 Self::HttpStatus { .. } => "http_status",
252 Self::Ssh { .. } => "ssh",
253 Self::Http { .. } => "http",
254 Self::HttpWorkerPanicked => "http_worker_panicked",
255 Self::HttpLowSpeedStall { .. } => "http_low_speed_stall",
256 Self::Credential { .. } => "credential",
257 Self::Bundle(_) => "bundle",
258 Self::Clone(_) => "clone",
259 Self::Snapshot(_) => "snapshot",
260 }
261 }
262
263 #[must_use]
265 pub fn area(&self) -> &'static str {
266 "remote"
267 }
268
269 #[must_use]
271 pub fn unreadable(url: impl Into<String>, reason: RemoteUnreadableReason) -> Self {
272 Self::Unreadable {
273 url: Some(url.into()),
274 reason,
275 }
276 }
277}