Various fixups:

- Don't PANIC but Result::Err() when casting an invalid
  PgVersionId to PgMajorVersion
- Fix the Display impl of postgres_versioninfo's error types
This commit is contained in:
Matthias van de Meent
2025-06-24 12:45:47 +02:00
parent eace983d73
commit 43b5175f8b
2 changed files with 19 additions and 7 deletions

View File

@@ -1,3 +1,4 @@
use serde::ser::SerializeTuple;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::fmt::{Display, Formatter};
@@ -128,10 +129,17 @@ impl Display for PgMajorVersion {
}
}
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
#[allow(dead_code)]
pub struct InvalidPgVersion(u32);
impl Display for InvalidPgVersion {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.serialize_tuple_struct("InvalidPgVersion", 1)?
.serialize_element(&self.0)
}
}
impl TryFrom<PgVersionId> for PgMajorVersion {
type Error = InvalidPgVersion;
@@ -141,9 +149,7 @@ impl TryFrom<PgVersionId> for PgMajorVersion {
15 => PgMajorVersion::PG15,
16 => PgMajorVersion::PG16,
17 => PgMajorVersion::PG17,
_ => {
panic!("Unknown PgVersionId: {value}");
}
_ => return Err(InvalidPgVersion(value.0)),
})
}
}
@@ -155,9 +161,15 @@ impl From<PgMajorVersion> for PgVersionId {
}
#[derive(Debug, PartialEq, Eq, thiserror::Error)]
#[error("PgMajorVersionParseError")]
pub struct PgMajorVersionParseError(String);
impl Display for PgMajorVersionParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.serialize_tuple_struct("PgMajorVersionParseError", 1)?
.serialize_element(&self.0)
}
}
impl FromStr for PgMajorVersion {
type Err = PgMajorVersionParseError;

View File

@@ -964,8 +964,8 @@ where
* because safekeepers parse WAL headers and the format
* may change between versions.
*/
if PgMajorVersion::try_from(msg.pg_version).unwrap()
!= PgMajorVersion::try_from(self.state.server.pg_version).unwrap()
if PgMajorVersion::try_from(msg.pg_version)?
!= PgMajorVersion::try_from(self.state.server.pg_version)?
&& self.state.server.pg_version != UNKNOWN_SERVER_VERSION
{
bail!(