fix(postgres): decode PgDatabaseError for ErrorResponse

This commit is contained in:
Austin Bonander 2024-08-21 13:17:17 -07:00
parent 77982cb407
commit 4fb2dcbe97
2 changed files with 13 additions and 3 deletions

View file

@ -98,7 +98,7 @@ impl PgStream {
match message.format {
BackendMessageFormat::ErrorResponse => {
// An error returned from the database server.
return Err(PgDatabaseError(message.decode()?).into());
return Err(message.decode::<PgDatabaseError>()?.into());
}
BackendMessageFormat::NotificationResponse => {

View file

@ -3,10 +3,10 @@ use std::fmt::{self, Debug, Display, Formatter};
use atoi::atoi;
use smallvec::alloc::borrow::Cow;
use sqlx_core::bytes::Bytes;
pub(crate) use sqlx_core::error::*;
use crate::message::{Notice, PgSeverity};
use crate::message::{BackendMessage, BackendMessageFormat, Notice, PgSeverity};
/// An error returned from the PostgreSQL database.
pub struct PgDatabaseError(pub(crate) Notice);
@ -219,6 +219,16 @@ impl DatabaseError for PgDatabaseError {
}
}
// ErrorResponse is the same structure as NoticeResponse but a different format code.
impl BackendMessage for PgDatabaseError {
const FORMAT: BackendMessageFormat = BackendMessageFormat::ErrorResponse;
#[inline(always)]
fn decode_body(buf: Bytes) -> std::result::Result<Self, Error> {
Ok(Self(Notice::decode_body(buf)?))
}
}
/// For reference: <https://www.postgresql.org/docs/current/errcodes-appendix.html>
pub(crate) mod error_codes {
/// Caused when a unique or primary key is violated.