Skip to content

Commit

Permalink
chore: unify error message for i32/i64 parsing (#4429)
Browse files Browse the repository at this point in the history
* chore: unify error message for i32/i64 parsing

* chore: extract common logic to "parse_number_as_i64"
  • Loading branch information
jkomyno authored Nov 13, 2023
1 parent 174e7d3 commit e0f0b06
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions query-engine/driver-adapters/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,20 @@ fn js_value_to_quaint(
column_type: ColumnType,
column_name: &str,
) -> quaint::Result<QuaintValue<'static>> {
let parse_number_as_i64 = |n: &serde_json::Number| {
n.as_i64().ok_or(conversion_error!(
"number must be an integer in column '{column_name}', got '{n}'"
))
};

// Note for the future: it may be worth revisiting how much bloat so many panics with different static
// strings add to the compiled artefact, and in case we should come up with a restricted set of panic
// messages, or even find a way of removing them altogether.
match column_type {
ColumnType::Int32 => match json_value {
serde_json::Value::Number(n) => {
// n.as_i32() is not implemented, so we need to downcast from i64 instead
n.as_i64()
.ok_or(conversion_error!("number must be an integer in column '{column_name}'"))
parse_number_as_i64(&n)
.and_then(|n| -> quaint::Result<i32> {
n.try_into()
.map_err(|e| conversion_error!("cannot convert {n} to i32 in column '{column_name}': {e}"))
Expand All @@ -273,9 +278,7 @@ fn js_value_to_quaint(
)),
},
ColumnType::Int64 => match json_value {
serde_json::Value::Number(n) => n.as_i64().map(QuaintValue::int64).ok_or(conversion_error!(
"number must be an i64 in column '{column_name}', got {n}"
)),
serde_json::Value::Number(n) => parse_number_as_i64(&n).map(QuaintValue::int64),
serde_json::Value::String(s) => s.parse::<i64>().map(QuaintValue::int64).map_err(|e| {
conversion_error!("string-encoded number must be an i64 in column '{column_name}', got {s}: {e}")
}),
Expand Down

0 comments on commit e0f0b06

Please sign in to comment.