Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(postgres): prefer parsing non-localized notice severity field #745

Merged
merged 1 commit into from
Oct 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 47 additions & 18 deletions sqlx-core/src/postgres/message/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@ impl PgSeverity {
}
}

impl std::convert::TryFrom<&str> for PgSeverity {
type Error = Error;

fn try_from(s: &str) -> Result<PgSeverity, Error> {
let result = match s {
"PANIC" => PgSeverity::Panic,
"FATAL" => PgSeverity::Fatal,
"ERROR" => PgSeverity::Error,
"WARNING" => PgSeverity::Warning,
"NOTICE" => PgSeverity::Notice,
"DEBUG" => PgSeverity::Debug,
"INFO" => PgSeverity::Info,
"LOG" => PgSeverity::Log,

severity => {
return Err(err_protocol!("unknown severity: {:?}", severity));
}
};

Ok(result)
}
}

#[derive(Debug)]
pub struct Notice {
storage: Bytes,
Expand Down Expand Up @@ -84,7 +107,12 @@ impl Notice {

impl Decode<'_> for Notice {
fn decode_with(buf: Bytes, _: ()) -> Result<Self, Error> {
let mut severity = PgSeverity::Log;
// In order to support PostgreSQL 9.5 and older we need to parse the localized S field.
// Newer versions additionally come with the V field that is guaranteed to be in English.
// We thus read both versions and prefer the unlocalized one if available.
const DEFAULT_SEVERITY: PgSeverity = PgSeverity::Log;
let mut severity_v = None;
let mut severity_s = None;
let mut message = (0, 0);
let mut code = (0, 0);

Expand All @@ -103,23 +131,24 @@ impl Decode<'_> for Notice {
break;
}

use std::convert::TryInto;
match field {
b'S' | b'V' => {
// unwrap: impossible to fail at this point
severity = match from_utf8(&buf[v.0 as usize..v.1 as usize]).unwrap() {
"PANIC" => PgSeverity::Panic,
"FATAL" => PgSeverity::Fatal,
"ERROR" => PgSeverity::Error,
"WARNING" => PgSeverity::Warning,
"NOTICE" => PgSeverity::Notice,
"DEBUG" => PgSeverity::Debug,
"INFO" => PgSeverity::Info,
"LOG" => PgSeverity::Log,

severity => {
return Err(err_protocol!("unknown severity: {:?}", severity));
}
};
b'S' => {
// Discard potential errors, because the message might be localized
severity_s = from_utf8(&buf[v.0 as usize..v.1 as usize])
.unwrap()
.try_into()
.ok();
}

b'V' => {
// Propagate errors here, because V is not localized and thus we are missing a possible
// variant.
severity_v = Some(
from_utf8(&buf[v.0 as usize..v.1 as usize])
.unwrap()
.try_into()?,
);
}

b'M' => {
Expand All @@ -135,7 +164,7 @@ impl Decode<'_> for Notice {
}

Ok(Self {
severity,
severity: severity_v.or(severity_s).unwrap_or(DEFAULT_SEVERITY),
message,
code,
storage: buf,
Expand Down