Skip to main content

git_remote_refs/
error.rs

1use crate::ObjectId;
2use alloc::string::String;
3use core::fmt;
4
5#[derive(Clone, Debug, PartialEq, Eq)]
6pub enum Error {
7    InvalidUrl,
8    InvalidRefName,
9    InvalidPrefix,
10    InvalidObjectId,
11    AuthRequired(u16),
12    NotFound,
13    Http(u16),
14    NotSmartHttp,
15    Protocol(&'static str),
16    Remote(String),
17    UnsupportedObjectFormat,
18    UnsupportedFeature(&'static str),
19    ChecksumMismatch,
20    LimitExceeded(&'static str),
21    MissingObject(ObjectId),
22}
23
24impl Error {
25    pub const fn code(&self) -> &'static str {
26        match self {
27            Self::InvalidUrl => "invalid-url",
28            Self::InvalidRefName => "invalid-ref-name",
29            Self::InvalidPrefix => "invalid-prefix",
30            Self::InvalidObjectId => "invalid-object-id",
31            Self::AuthRequired(_) => "auth-required",
32            Self::NotFound => "not-found",
33            Self::Http(_) => "http",
34            Self::NotSmartHttp => "not-smart-http",
35            Self::Protocol(_) => "protocol",
36            Self::Remote(_) => "remote",
37            Self::UnsupportedObjectFormat => "unsupported-object-format",
38            Self::UnsupportedFeature(_) => "unsupported-feature",
39            Self::ChecksumMismatch => "checksum-mismatch",
40            Self::LimitExceeded(_) => "limit-exceeded",
41            Self::MissingObject(_) => "missing-object",
42        }
43    }
44
45    pub const fn status(&self) -> Option<u16> {
46        match self {
47            Self::AuthRequired(status) | Self::Http(status) => Some(*status),
48            Self::NotFound => Some(404),
49            _ => None,
50        }
51    }
52}
53
54impl fmt::Display for Error {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        match self {
57            Self::InvalidUrl => {
58                f.write_str("expected an HTTP(S) repository URL without query or fragment")
59            }
60            Self::InvalidRefName => f.write_str("invalid ref name"),
61            Self::InvalidPrefix => f.write_str("invalid ref prefix"),
62            Self::InvalidObjectId => f.write_str("expected a full hexadecimal object ID"),
63            Self::AuthRequired(status) => write!(
64                f,
65                "repository requires authentication or does not exist (HTTP {status})"
66            ),
67            Self::NotFound => f.write_str("repository not found (HTTP 404)"),
68            Self::Http(status) => write!(f, "Git server returned HTTP {status}"),
69            Self::NotSmartHttp => f.write_str("response is not a Git smart HTTP response"),
70            Self::Protocol(message) => write!(f, "Git protocol error: {message}"),
71            Self::Remote(message) => write!(f, "Git server error: {message}"),
72            Self::UnsupportedObjectFormat => f.write_str("unsupported object format"),
73            Self::UnsupportedFeature(feature) => write!(f, "server does not support {feature}"),
74            Self::ChecksumMismatch => f.write_str("pack checksum mismatch"),
75            Self::LimitExceeded(limit) => write!(f, "limit exceeded: {limit}"),
76            Self::MissingObject(oid) => write!(f, "server did not send object {oid}"),
77        }
78    }
79}
80
81impl core::error::Error for Error {}