Skip to content

Commit d90fae8

Browse files
committed
return error
1 parent 262d5eb commit d90fae8

4 files changed

Lines changed: 32 additions & 21 deletions

File tree

core/core/src/raw/http_util/bytes_content_range.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,16 @@ impl BytesContentRange {
6060
/// Update BytesContentRange with range.
6161
///
6262
/// The range is inclusive: `[start..=end]` as described in `content-range`.
63-
pub fn with_range(mut self, start: u64, end: u64) -> Self {
63+
pub fn with_range(mut self, start: u64, end: u64) -> Result<Self> {
64+
if end < start {
65+
return Err(Error::new(
66+
ErrorKind::Unexpected,
67+
format!("invalid BytesContentRange: end ({end}) < start ({start})"),
68+
));
69+
}
6470
self.0 = Some(start);
6571
self.1 = Some(end);
66-
assert!(
67-
end >= start,
68-
"invalid BytesContentRange: end ({end}) < start ({start})"
69-
);
70-
self
72+
Ok(self)
7173
}
7274

7375
/// Update BytesContentRange with size.
@@ -175,7 +177,7 @@ impl FromStr for BytesContentRange {
175177
.with_operation("BytesContentRange::from_str")
176178
.with_context("value", value));
177179
}
178-
let mut bcr = BytesContentRange::default().with_range(start, end);
180+
let mut bcr = BytesContentRange::default().with_range(start, end)?;
179181

180182
// Handle size part first.
181183
if s[1] != "*" {
@@ -196,13 +198,13 @@ mod tests {
196198
(
197199
"range start with unknown size",
198200
"bytes 123-123/*",
199-
BytesContentRange::default().with_range(123, 123),
201+
BytesContentRange::default().with_range(123, 123)?,
200202
),
201203
(
202204
"range start with known size",
203205
"bytes 123-123/1024",
204206
BytesContentRange::default()
205-
.with_range(123, 123)
207+
.with_range(123, 123)?
206208
.with_size(1024),
207209
),
208210
(
@@ -222,36 +224,45 @@ mod tests {
222224
}
223225

224226
#[test]
225-
fn test_bytes_content_range_to_string() {
227+
fn test_bytes_content_range_to_string() -> Result<()> {
226228
let h = BytesContentRange::default().with_size(1024);
227229
assert_eq!(h.to_string(), "*/1024");
228230

229-
let h = BytesContentRange::default().with_range(0, 1023);
231+
let h = BytesContentRange::default().with_range(0, 1023)?;
230232
assert_eq!(h.to_string(), "0-1023/*");
231233

232234
let h = BytesContentRange::default()
233-
.with_range(0, 1023)
235+
.with_range(0, 1023)?
234236
.with_size(1024);
235237
assert_eq!(h.to_string(), "0-1023/1024");
238+
239+
Ok(())
236240
}
237241

238242
#[test]
239-
fn test_bytes_content_range_to_header() {
243+
fn test_bytes_content_range_to_header() -> Result<()> {
240244
let h = BytesContentRange::default().with_size(1024);
241245
assert_eq!(h.to_header(), "bytes */1024");
242246

243-
let h = BytesContentRange::default().with_range(0, 1023);
247+
let h = BytesContentRange::default().with_range(0, 1023)?;
244248
assert_eq!(h.to_header(), "bytes 0-1023/*");
245249

246250
let h = BytesContentRange::default()
247-
.with_range(0, 1023)
251+
.with_range(0, 1023)?
248252
.with_size(1024);
249253
assert_eq!(h.to_header(), "bytes 0-1023/1024");
254+
255+
Ok(())
250256
}
251257

252258
#[test]
253-
#[should_panic(expected = "invalid BytesContentRange: end (50) < start (100)")]
254-
fn test_bytes_content_range_len_panics_on_inverted_range() {
255-
let _ = BytesContentRange::default().with_range(100, 50);
259+
fn test_bytes_content_range_inverted_range_returns_error() {
260+
let err = BytesContentRange::default()
261+
.with_range(100, 50)
262+
.unwrap_err();
263+
assert!(
264+
err.to_string()
265+
.contains("invalid BytesContentRange: end (50) < start (100)")
266+
);
256267
}
257268
}

core/core/src/raw/http_util/bytes_range.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl BytesRange {
7979
if n > size {
8080
return Err(Error::new(
8181
ErrorKind::Unexpected,
82-
&format!("cannot advance BytesRange by {n} bytes, only {size} bytes left"),
82+
format!("cannot advance BytesRange by {n} bytes, only {size} bytes left"),
8383
));
8484
}
8585
}

core/layers/foyer/src/full.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<A: Access> FullReader<A> {
115115
Ok(entry) => {
116116
let end = range_end.unwrap_or(entry.len() as u64);
117117
let range = BytesContentRange::default()
118-
.with_range(range_start, end - 1)
118+
.with_range(range_start, end - 1)?
119119
.with_size(entry.len() as _);
120120
let buffer = entry.slice(range_start as usize..end as usize);
121121
let rp = RpRead::new()

core/services/ghac/src/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl GhacCore {
303303
req = req.header(
304304
CONTENT_RANGE,
305305
BytesContentRange::default()
306-
.with_range(offset, offset + size - 1)
306+
.with_range(offset, offset + size - 1)?
307307
.to_header(),
308308
);
309309
let req = req

0 commit comments

Comments
 (0)