Skip to main content

lfsx_server/
error.rs

1use axum::Json;
2use axum::http::{HeaderValue, StatusCode, header};
3use axum::response::{IntoResponse, Response};
4use serde_json::json;
5
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8    #[error("object id is not a lowercase hex sha256 digest")]
9    MalformedOid,
10
11    #[error("organisation and repository must be plain names")]
12    MalformedNamespace,
13
14    #[error("content hashes to {actual}, which does not match the declared object id {declared}")]
15    OidMismatch { declared: String, actual: String },
16
17    #[error("content is {actual} bytes, but {declared} were declared")]
18    SizeMismatch { declared: u64, actual: u64 },
19
20    #[error("object exceeds the {limit} byte limit this server accepts")]
21    TooLarge { limit: u64 },
22
23    #[error("this repository holds {used} bytes of its {limit} byte budget")]
24    OverQuota { used: u64, limit: u64 },
25
26    #[error("this server does not compress objects — set LFSX_COMPRESSION first")]
27    CompressionDisabled,
28
29    #[error("credentials are required for this repository")]
30    Unauthenticated,
31
32    #[error("these credentials do not grant that access to this repository")]
33    Forbidden,
34
35    #[error("the forge could not be reached to check permissions")]
36    Forge,
37
38    #[error("lock path must not be empty")]
39    MalformedLockPath,
40
41    #[error("the file is already locked")]
42    LockHeld(Box<crate::locks::Lock>),
43
44    #[error("lock not found")]
45    LockNotFound,
46
47    #[error("object not found")]
48    NotFound,
49
50    #[error("storage failure: {0}")]
51    Storage(#[from] std::io::Error),
52
53    #[error("could not serialise: {0}")]
54    Serialisation(#[from] serde_json::Error),
55}
56
57const CHALLENGE: HeaderValue = HeaderValue::from_static("Basic realm=\"Git LFS\"");
58
59impl Error {
60    fn status(&self) -> StatusCode {
61        match self {
62            Self::MalformedOid
63            | Self::MalformedLockPath
64            | Self::MalformedNamespace
65            | Self::OidMismatch { .. }
66            | Self::SizeMismatch { .. } => StatusCode::UNPROCESSABLE_ENTITY,
67            Self::TooLarge { .. } => StatusCode::PAYLOAD_TOO_LARGE,
68            Self::OverQuota { .. } => StatusCode::INSUFFICIENT_STORAGE,
69            Self::CompressionDisabled => StatusCode::CONFLICT,
70            Self::Unauthenticated => StatusCode::UNAUTHORIZED,
71            Self::Forbidden => StatusCode::FORBIDDEN,
72            Self::LockHeld(_) => StatusCode::CONFLICT,
73            Self::NotFound | Self::LockNotFound => StatusCode::NOT_FOUND,
74            Self::Forge => StatusCode::BAD_GATEWAY,
75            Self::Storage(_) | Self::Serialisation(_) => StatusCode::INTERNAL_SERVER_ERROR,
76        }
77    }
78}
79
80impl Error {
81    fn cause(&self) -> &'static str {
82        match self {
83            Self::MalformedOid => "malformed_oid",
84            Self::MalformedNamespace => "malformed_namespace",
85            Self::MalformedLockPath => "malformed_lock_path",
86            Self::OidMismatch { .. } => "oid_mismatch",
87            Self::SizeMismatch { .. } => "size_mismatch",
88            Self::TooLarge { .. } => "too_large",
89            Self::OverQuota { .. } => "over_quota",
90            Self::CompressionDisabled => "compression_disabled",
91            Self::Unauthenticated => "unauthenticated",
92            Self::Forbidden => "forbidden",
93            Self::Forge => "forge_unreachable",
94            Self::LockHeld(_) => "lock_held",
95            Self::LockNotFound => "lock_not_found",
96            Self::NotFound => "not_found",
97            Self::Storage(_) => "storage",
98            Self::Serialisation(_) => "serialisation",
99        }
100    }
101}
102
103impl IntoResponse for Error {
104    fn into_response(self) -> Response {
105        let status = self.status();
106        let cause = crate::metrics::Cause(self.cause());
107
108        if status.is_server_error() {
109            tracing::error!(error = %self, "request failed");
110        }
111
112        if let Self::LockHeld(lock) = &self {
113            let mut response = (
114                status,
115                Json(json!({ "lock": lock, "message": self.to_string() })),
116            )
117                .into_response();
118            response.extensions_mut().insert(cause);
119            return response;
120        }
121
122        let body = Json(json!({ "message": self.to_string() }));
123        let mut response = if status == StatusCode::UNAUTHORIZED {
124            (
125                status,
126                [
127                    (header::WWW_AUTHENTICATE, CHALLENGE),
128                    (
129                        header::HeaderName::from_static("lfs-authenticate"),
130                        CHALLENGE,
131                    ),
132                ],
133                body,
134            )
135                .into_response()
136        } else {
137            (status, body).into_response()
138        };
139
140        response.extensions_mut().insert(cause);
141        response
142    }
143}