parse server error messages (#543)

This commit adds a parser to the Postgres error message, providing better
error messages.

Implemented based in:
  https://www.postgresql.org/docs/12/protocol-error-fields.html

Signed-off-by: Sebastian Webber <sebastian@swebber.me>
This commit is contained in:
Sebastian Webber
2023-08-09 13:14:05 -03:00
committed by GitHub
parent 1cde74f05e
commit 9ab128579d
3 changed files with 308 additions and 6 deletions

View File

@@ -588,8 +588,7 @@ impl Server {
// An error message will be present.
_ => {
// Read the error message without the terminating null character.
let mut error = vec![0u8; len as usize - 4 - 1];
let mut error = vec![0u8; len as usize];
match stream.read_exact(&mut error).await {
Ok(_) => (),
@@ -601,10 +600,14 @@ impl Server {
}
};
// TODO: the error message contains multiple fields; we can decode them and
// present a prettier message to the user.
// See: https://www.postgresql.org/docs/12/protocol-error-fields.html
error!("Server error: {}", String::from_utf8_lossy(&error));
let fields = match PgErrorMsg::parse(error) {
Ok(f) => f,
Err(err) => {
return Err(err);
}
};
trace!("error fields: {}", &fields);
error!("server error: {}: {}", fields.severity, fields.message);
}
};