Skip to content

Commit 7d9fed6

Browse files
committed
no unwrap, return err instead
1 parent f8b9fa8 commit 7d9fed6

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

nexus/db-queries/src/db/queries/region_allocation.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ pub enum AllocationQueryError {
109109

110110
/// Adding the overhead to the requested size overflowed
111111
RequestedRegionOverheadOverflow { request: i64, overhead: i64 },
112+
113+
/// Converting from u64 to i64 truncated
114+
RequestedRegionSizeTruncated { request: u64, e: String },
112115
}
113116

114117
impl From<AllocationQueryError> for external::Error {
@@ -146,6 +149,13 @@ impl From<AllocationQueryError> for external::Error {
146149
"adding {overhead} to region size {request} overflowed"
147150
),
148151
),
152+
153+
AllocationQueryError::RequestedRegionSizeTruncated {
154+
request,
155+
e,
156+
} => external::Error::internal_error(&format!(
157+
"converting {request} to i64 failed! {e}"
158+
)),
149159
}
150160
}
151161
}
@@ -200,9 +210,17 @@ pub fn allocation_query(
200210
});
201211
}
202212

203-
// After the above check, unconditionally cast from u64 to i64. The value is
204-
// low enough that this shouldn't truncate.
205-
let requested_size: i64 = requested_size.try_into().unwrap();
213+
// After the above check, cast from u64 to i64. The value is low enough
214+
// (after the check above) that try_into should always return Ok.
215+
let requested_size: i64 = match requested_size.try_into() {
216+
Ok(v) => v,
217+
Err(e) => {
218+
return Err(AllocationQueryError::RequestedRegionSizeTruncated {
219+
request: requested_size,
220+
e: e.to_string(),
221+
});
222+
}
223+
};
206224

207225
let reservation_percent = RegionReservationPercent::TwentyFive;
208226

0 commit comments

Comments
 (0)