From dfb60141bd371e0cecadc70414094c77f286c3ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Gaspard?= Date: Sun, 11 Feb 2024 18:11:48 +0100 Subject: [PATCH] fix: do not panic when binding a large BigDecimal --- sqlx-postgres/src/types/bigdecimal.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/sqlx-postgres/src/types/bigdecimal.rs b/sqlx-postgres/src/types/bigdecimal.rs index 8240bb0d2..9d66d359d 100644 --- a/sqlx-postgres/src/types/bigdecimal.rs +++ b/sqlx-postgres/src/types/bigdecimal.rs @@ -138,12 +138,18 @@ impl TryFrom<&'_ BigDecimal> for PgNumeric { } } -/// ### Panics -/// If this `BigDecimal` cannot be represented by `PgNumeric`. impl Encode<'_, Postgres> for BigDecimal { fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull { + use std::str::FromStr; + // If the argument is too big, then we replace it with a less big argument. + // This less big argument is already outside the range of allowed PostgreSQL DECIMAL, which + // means that PostgreSQL will return the 22P03 error kind upon receiving it. This is the + // expected error, and the user should be ready to handle it anyway. PgNumeric::try_from(self) - .expect("BigDecimal magnitude too great for Postgres NUMERIC type") + .unwrap_or_else(|_| { + PgNumeric::try_from(&BigDecimal::from_str(&format!("{:030000}", 0)).unwrap()) + .unwrap() + }) .encode(buf); IsNull::No