Skip to content

Commit d3e75e7

Browse files
committed
fix(postgres): decode PgDatabaseError for ErrorResponse
1 parent 2e9ba07 commit d3e75e7

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

sqlx-postgres/src/connection/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl PgStream {
9898
match message.format {
9999
BackendMessageFormat::ErrorResponse => {
100100
// An error returned from the database server.
101-
return Err(PgDatabaseError(message.decode()?).into());
101+
return Err(message.decode::<PgDatabaseError>()?.into());
102102
}
103103

104104
BackendMessageFormat::NotificationResponse => {

sqlx-postgres/src/error.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use std::fmt::{self, Debug, Display, Formatter};
33

44
use atoi::atoi;
55
use smallvec::alloc::borrow::Cow;
6-
6+
use sqlx_core::bytes::Bytes;
77
pub(crate) use sqlx_core::error::*;
88

9-
use crate::message::{Notice, PgSeverity};
9+
use crate::message::{BackendMessage, BackendMessageFormat, Notice, PgSeverity};
1010

1111
/// An error returned from the PostgreSQL database.
1212
pub struct PgDatabaseError(pub(crate) Notice);
@@ -219,6 +219,16 @@ impl DatabaseError for PgDatabaseError {
219219
}
220220
}
221221

222+
// ErrorResponse is the same structure as NoticeResponse but a different format code.
223+
impl BackendMessage for PgDatabaseError {
224+
const FORMAT: BackendMessageFormat = BackendMessageFormat::ErrorResponse;
225+
226+
#[inline(always)]
227+
fn decode_body(buf: Bytes) -> std::result::Result<Self, Error> {
228+
Ok(Self(Notice::decode_body(buf)?))
229+
}
230+
}
231+
222232
/// For reference: <https://www.postgresql.org/docs/current/errcodes-appendix.html>
223233
pub(crate) mod error_codes {
224234
/// Caused when a unique or primary key is violated.

0 commit comments

Comments
 (0)