From 43b5175f8b1c101cca7d7e9d9b2dc38dffcfb219 Mon Sep 17 00:00:00 2001 From: Matthias van de Meent Date: Tue, 24 Jun 2025 12:45:47 +0200 Subject: [PATCH] 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 --- libs/postgres_versioninfo/src/lib.rs | 22 +++++++++++++++++----- safekeeper/src/safekeeper.rs | 4 ++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/libs/postgres_versioninfo/src/lib.rs b/libs/postgres_versioninfo/src/lib.rs index 7a29f540fa..1324899724 100644 --- a/libs/postgres_versioninfo/src/lib.rs +++ b/libs/postgres_versioninfo/src/lib.rs @@ -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 for PgMajorVersion { type Error = InvalidPgVersion; @@ -141,9 +149,7 @@ impl TryFrom 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 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; diff --git a/safekeeper/src/safekeeper.rs b/safekeeper/src/safekeeper.rs index 02dda7ae8e..314c53adb2 100644 --- a/safekeeper/src/safekeeper.rs +++ b/safekeeper/src/safekeeper.rs @@ -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!(