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("credentials are required for this repository")]
24 Unauthenticated,
25
26 #[error("these credentials do not grant that access to this repository")]
27 Forbidden,
28
29 #[error("the forge could not be reached to check permissions")]
30 Forge,
31
32 #[error("lock path must not be empty")]
33 MalformedLockPath,
34
35 #[error("the file is already locked")]
36 LockHeld(Box<crate::locks::Lock>),
37
38 #[error("lock not found")]
39 LockNotFound,
40
41 #[error("object not found")]
42 NotFound,
43
44 #[error("storage failure: {0}")]
45 Storage(#[from] std::io::Error),
46
47 #[error("could not serialise: {0}")]
48 Serialisation(#[from] serde_json::Error),
49}
50
51const CHALLENGE: HeaderValue = HeaderValue::from_static("Basic realm=\"Git LFS\"");
52
53impl Error {
54 fn status(&self) -> StatusCode {
55 match self {
56 Self::MalformedOid
57 | Self::MalformedLockPath
58 | Self::MalformedNamespace
59 | Self::OidMismatch { .. }
60 | Self::SizeMismatch { .. } => StatusCode::UNPROCESSABLE_ENTITY,
61 Self::TooLarge { .. } => StatusCode::PAYLOAD_TOO_LARGE,
62 Self::Unauthenticated => StatusCode::UNAUTHORIZED,
63 Self::Forbidden => StatusCode::FORBIDDEN,
64 Self::LockHeld(_) => StatusCode::CONFLICT,
65 Self::NotFound | Self::LockNotFound => StatusCode::NOT_FOUND,
66 Self::Forge => StatusCode::BAD_GATEWAY,
67 Self::Storage(_) | Self::Serialisation(_) => StatusCode::INTERNAL_SERVER_ERROR,
68 }
69 }
70}
71
72impl Error {
73 fn cause(&self) -> &'static str {
74 match self {
75 Self::MalformedOid => "malformed_oid",
76 Self::MalformedNamespace => "malformed_namespace",
77 Self::MalformedLockPath => "malformed_lock_path",
78 Self::OidMismatch { .. } => "oid_mismatch",
79 Self::SizeMismatch { .. } => "size_mismatch",
80 Self::TooLarge { .. } => "too_large",
81 Self::Unauthenticated => "unauthenticated",
82 Self::Forbidden => "forbidden",
83 Self::Forge => "forge_unreachable",
84 Self::LockHeld(_) => "lock_held",
85 Self::LockNotFound => "lock_not_found",
86 Self::NotFound => "not_found",
87 Self::Storage(_) => "storage",
88 Self::Serialisation(_) => "serialisation",
89 }
90 }
91}
92
93impl IntoResponse for Error {
94 fn into_response(self) -> Response {
95 let status = self.status();
96 let cause = crate::metrics::Cause(self.cause());
97
98 if status.is_server_error() {
99 tracing::error!(error = %self, "request failed");
100 }
101
102 if let Self::LockHeld(lock) = &self {
103 let mut response = (
104 status,
105 Json(json!({ "lock": lock, "message": self.to_string() })),
106 )
107 .into_response();
108 response.extensions_mut().insert(cause);
109 return response;
110 }
111
112 let body = Json(json!({ "message": self.to_string() }));
113 let mut response = if status == StatusCode::UNAUTHORIZED {
114 (
115 status,
116 [
117 (header::WWW_AUTHENTICATE, CHALLENGE),
118 (
119 header::HeaderName::from_static("lfs-authenticate"),
120 CHALLENGE,
121 ),
122 ],
123 body,
124 )
125 .into_response()
126 } else {
127 (status, body).into_response()
128 };
129
130 response.extensions_mut().insert(cause);
131 response
132 }
133}