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("method not allowed")]
65 MethodNotAllowed,
66
67 #[error("lock acquisition timed out for upload {0}")]
69 LockTimeout(String),
70
71 #[error("lock is already held for upload {0}")]
72 LockConflict(String),
73
74 #[error("hook rejected request: {0}")]
76 HookRejected(String),
77
78 #[error("I/O error: {0}")]
80 Io(#[from] std::io::Error),
81
82 #[error("serialization error: {0}")]
83 Serialization(#[from] serde_json::Error),
84
85 #[error("internal error: {0}")]
86 Internal(String),
87}
88
89impl TusError {
90 pub fn status_code(&self) -> StatusCode {
92 match self {
93 Self::MissingTusResumable => StatusCode::PRECONDITION_FAILED,
94 Self::UnsupportedVersion { .. } => StatusCode::PRECONDITION_FAILED,
95 Self::MissingUploadOffset => StatusCode::BAD_REQUEST,
96 Self::OffsetMismatch { .. } => StatusCode::CONFLICT,
97 Self::WrongContentType(_) => StatusCode::UNSUPPORTED_MEDIA_TYPE,
98 Self::NotFound(_) => StatusCode::NOT_FOUND,
99 Self::Gone => StatusCode::GONE,
100 Self::EntityTooLarge { .. } => StatusCode::PAYLOAD_TOO_LARGE,
101 Self::ExceedsUploadLength { .. } => StatusCode::PAYLOAD_TOO_LARGE,
102 Self::ChecksumMismatch => match StatusCode::from_u16(460) {
104 Ok(s) => s,
105 Err(_) => StatusCode::INTERNAL_SERVER_ERROR,
106 },
107 Self::UnsupportedChecksumAlgorithm(_) => StatusCode::BAD_REQUEST,
108 Self::MissingUploadLength => StatusCode::BAD_REQUEST,
109 Self::UploadLengthAlreadySet => StatusCode::BAD_REQUEST,
110 Self::ExtensionNotEnabled(_) => StatusCode::NOT_FOUND,
111 Self::InvalidMetadata(_) => StatusCode::BAD_REQUEST,
112 Self::InvalidUploadId => StatusCode::BAD_REQUEST,
113 Self::EmptyConcatenation => StatusCode::BAD_REQUEST,
114 Self::PartialUploadIncomplete(_) => StatusCode::BAD_REQUEST,
115 Self::PatchOnFinalUpload => StatusCode::FORBIDDEN,
116 Self::MethodNotAllowed => StatusCode::METHOD_NOT_ALLOWED,
117 Self::LockTimeout(_) => StatusCode::REQUEST_TIMEOUT,
118 Self::LockConflict(_) => StatusCode::LOCKED,
119 Self::HookRejected(_) => StatusCode::FORBIDDEN,
120 Self::Io(_) => StatusCode::INTERNAL_SERVER_ERROR,
121 Self::Serialization(_) => StatusCode::INTERNAL_SERVER_ERROR,
122 Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
123 }
124 }
125}
126
127#[cfg(test)]
128mod tests {
129 use super::*;
130
131 #[test]
133 fn status_code_mapping() {
134 let cases: &[(TusError, u16)] = &[
135 (TusError::MissingTusResumable, 412),
136 (TusError::UnsupportedVersion { version: "0.9".into() }, 412),
137 (TusError::MissingUploadOffset, 400),
138 (TusError::OffsetMismatch { expected: 10, actual: 5 }, 409),
139 (TusError::WrongContentType("text/plain".into()), 415),
140 (TusError::NotFound("abc".into()), 404),
141 (TusError::Gone, 410),
142 (TusError::EntityTooLarge { max: 1024 }, 413),
143 (
144 TusError::ExceedsUploadLength {
145 declared: 10,
146 end: 20,
147 },
148 413,
149 ),
150 (TusError::ChecksumMismatch, 460),
151 (TusError::UnsupportedChecksumAlgorithm("crc32".into()), 400),
152 (TusError::MissingUploadLength, 400),
153 (TusError::UploadLengthAlreadySet, 400),
154 (TusError::ExtensionNotEnabled("concatenation"), 404),
155 (TusError::InvalidMetadata("bad base64".into()), 400),
156 (TusError::InvalidUploadId, 400),
157 (TusError::EmptyConcatenation, 400),
158 (TusError::PartialUploadIncomplete("id1".into()), 400),
159 (TusError::PatchOnFinalUpload, 403),
160 (TusError::MethodNotAllowed, 405),
161 (TusError::LockTimeout("id1".into()), 408),
162 (TusError::LockConflict("id1".into()), 423),
163 (TusError::HookRejected("not allowed".into()), 403),
164 (TusError::Io(std::io::Error::other("disk full")), 500),
165 (TusError::Serialization(serde_json::from_str::<()>("!").unwrap_err()), 500),
166 (TusError::Internal("oops".into()), 500),
167 ];
168
169 for (err, expected_status) in cases {
170 assert_eq!(
171 err.status_code().as_u16(),
172 *expected_status,
173 "wrong status for: {err}"
174 );
175 }
176 }
177}