Port error check from https://github.com/canonical/postgresql-operator/pull/1328 to 16/edge.
Port error check from #1328 to 16/edge.