1use http::StatusCode;
2use thiserror::Error;
3
4#[derive(Debug, Error)]
5pub enum TusError {
6 #[error("missing Tus-Resumable header")]
8 MissingTusResumable,
9
10 #[error("unsupported tus version: {version}")]
11 UnsupportedVersion { version: String },
12
13 #[error("missing Upload-Offset header")]
14 MissingUploadOffset,
15
16 #[error("upload offset mismatch: expected {expected}, got {actual}")]
17 OffsetMismatch { expected: u64, actual: u64 },
18
19 #[error("wrong Content-Type: expected application/offset+octet-stream, got {0}")]
20 WrongContentType(String),
21
22 #[error("upload not found: {0}")]
23 NotFound(String),
24
25 #[error("upload has expired")]
26 Gone,
27
28 #[error("upload size exceeds server maximum of {max} bytes")]
29 EntityTooLarge { max: u64 },
30
31 #[error("chunk exceeds declared upload length (declared {declared} bytes, end offset would be {end})")]
32 ExceedsUploadLength { declared: u64, end: u64 },
33
34 #[error("checksum mismatch")]
35 ChecksumMismatch,
36
37 #[error("unsupported checksum algorithm: {0}")]
38 UnsupportedChecksumAlgorithm(String),
39
40 #[error("missing Upload-Length (or Upload-Defer-Length)")]
41 MissingUploadLength,
42
43 #[error("Upload-Length cannot be changed once set")]
44 UploadLengthAlreadySet,
45
46 #[error("extension not enabled: {0}")]
47 ExtensionNotEnabled(&'static str),
48
49 #[error("invalid metadata: {0}")]
50 InvalidMetadata(String),
51
52 #[error("invalid upload ID")]
53 InvalidUploadId,
54
55 #[error("concatenation requires at least one partial upload URL")]
56 EmptyConcatenation,
57
58 #[error("partial upload {0} is not yet complete")]
59 PartialUploadIncomplete(String),
60
61 #[error("PATCH is not allowed on a final concatenated upload")]
62 PatchOnFinalUpload,
63
64 #[error("upload is not complete or not available for download")]
65 UploadNotReadyForDownload,
66
67 #[error("method not allowed")]
68 MethodNotAllowed,
69
70 #[error("lock acquisition timed out for upload {0}")]
72 LockTimeout(String),
73
74 #[error("lock is already held for upload {0}")]
75 LockConflict(String),
76
77 #[error("hook rejected request: {0}")]
79 HookRejected(String),
80
81 #[error("I/O error: {0}")]
83 Io(#[from] std::io::Error),
84
85 #[error("serialization error: {0}")]
86 Serialization(#[from] serde_json::Error),
87
88 #[error("internal error: {0}")]
89 Internal(String),
90}
91
92impl TusError {
93 pub fn status_code(&self) -> StatusCode {
95 match self {
96 Self::MissingTusResumable => StatusCode::PRECONDITION_FAILED,
97 Self::UnsupportedVersion { .. } => StatusCode::PRECONDITION_FAILED,
98 Self::MissingUploadOffset => StatusCode::BAD_REQUEST,
99 Self::OffsetMismatch { .. } => StatusCode::CONFLICT,
100 Self::WrongContentType(_) => StatusCode::UNSUPPORTED_MEDIA_TYPE,
101 Self::NotFound(_) => StatusCode::NOT_FOUND,
102 Self::Gone => StatusCode::GONE,
103 Self::EntityTooLarge { .. } => StatusCode::PAYLOAD_TOO_LARGE,
104 Self::ExceedsUploadLength { .. } => StatusCode::PAYLOAD_TOO_LARGE,
105 Self::ChecksumMismatch => match StatusCode::from_u16(460) {
107 Ok(s) => s,
108 Err(_) => StatusCode::INTERNAL_SERVER_ERROR,
109 },
110 Self::UnsupportedChecksumAlgorithm(_) => StatusCode::BAD_REQUEST,
111 Self::MissingUploadLength => StatusCode::BAD_REQUEST,
112 Self::UploadLengthAlreadySet => StatusCode::BAD_REQUEST,
113 Self::ExtensionNotEnabled(_) => StatusCode::NOT_FOUND,
114 Self::InvalidMetadata(_) => StatusCode::BAD_REQUEST,
115 Self::InvalidUploadId => StatusCode::BAD_REQUEST,
116 Self::EmptyConcatenation => StatusCode::BAD_REQUEST,
117 Self::PartialUploadIncomplete(_) => StatusCode::BAD_REQUEST,
118 Self::PatchOnFinalUpload => StatusCode::FORBIDDEN,
119 Self::UploadNotReadyForDownload => StatusCode::BAD_REQUEST,
120 Self::MethodNotAllowed => StatusCode::METHOD_NOT_ALLOWED,
121 Self::LockTimeout(_) => StatusCode::REQUEST_TIMEOUT,
122 Self::LockConflict(_) => StatusCode::LOCKED,
123 Self::HookRejected(_) => StatusCode::FORBIDDEN,
124 Self::Io(_) => StatusCode::INTERNAL_SERVER_ERROR,
125 Self::Serialization(_) => StatusCode::INTERNAL_SERVER_ERROR,
126 Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
127 }
128 }
129}
130
131#[cfg(test)]
132mod tests {
133 use super::*;
134
135 #[test]
137 fn status_code_mapping() {
138 let cases: &[(TusError, u16)] = &[
139 (TusError::MissingTusResumable, 412),
140 (
141 TusError::UnsupportedVersion {
142 version: "0.9".into(),
143 },
144 412,
145 ),
146 (TusError::MissingUploadOffset, 400),
147 (
148 TusError::OffsetMismatch {
149 expected: 10,
150 actual: 5,
151 },
152 409,
153 ),
154 (TusError::WrongContentType("text/plain".into()), 415),
155 (TusError::NotFound("abc".into()), 404),
156 (TusError::Gone, 410),
157 (TusError::EntityTooLarge { max: 1024 }, 413),
158 (
159 TusError::ExceedsUploadLength {
160 declared: 10,
161 end: 20,
162 },
163 413,
164 ),
165 (TusError::ChecksumMismatch, 460),
166 (TusError::UnsupportedChecksumAlgorithm("crc32".into()), 400),
167 (TusError::MissingUploadLength, 400),
168 (TusError::UploadLengthAlreadySet, 400),
169 (TusError::ExtensionNotEnabled("concatenation"), 404),
170 (TusError::InvalidMetadata("bad base64".into()), 400),
171 (TusError::InvalidUploadId, 400),
172 (TusError::EmptyConcatenation, 400),
173 (TusError::PartialUploadIncomplete("id1".into()), 400),
174 (TusError::PatchOnFinalUpload, 403),
175 (TusError::UploadNotReadyForDownload, 400),
176 (TusError::MethodNotAllowed, 405),
177 (TusError::LockTimeout("id1".into()), 408),
178 (TusError::LockConflict("id1".into()), 423),
179 (TusError::HookRejected("not allowed".into()), 403),
180 (TusError::Io(std::io::Error::other("disk full")), 500),
181 (
182 TusError::Serialization(serde_json::from_str::<()>("!").unwrap_err()),
183 500,
184 ),
185 (TusError::Internal("oops".into()), 500),
186 ];
187
188 for (err, expected_status) in cases {
189 assert_eq!(
190 err.status_code().as_u16(),
191 *expected_status,
192 "wrong status for: {err}"
193 );
194 }
195 }
196}