ajmurmann
ajmurmann

Reputation: 1635

Cannot get description of postgres error?

I am writing to Postgres using the Rust Postgres crate. I expect that I will attempt to write some records that violate uniqueness constraints. If that happens I want to print a notice and keep going. I am trying to take a look at attributes of the errors that get returned to be able to differentiate between different errors going forward. A obvious place to start seem the cause and description methods on the error. However, when I try to call them the compiler claims they don't exist.

Here is the relevant part of my current code:

match result {
    Result::Ok(val) => println!("written to DB"),
    Result::Err(err) => {
        println!("Error description: {}", err.description());
        panic!("called `Result::unwrap()` on an `Err` value: {:?}");
    }
};

Compilation results in the following error:

error: no method named `description` found for type `postgres::error::Error` in the current scope
              println!("Error description: {}", err.description());

The documentation claims the method exists.

Upvotes: 0

Views: 1073

Answers (1)

Shepmaster
Shepmaster

Reputation: 431379

Since you did not provide an MCVE, I had to produce this one:

extern crate postgres;

fn main() {
    let result: Result<_, postgres::error::Error> = Ok(());

    match result {
        Result::Ok(val) => println!("written to DB"),
        Result::Err(err) => {
            panic!("Error description: {}", err.description());
        }
    };
}

If you read the entire error message (with some line numbers removed):

error: no method named `description` found for type `postgres::error::Error` in the current scope
 --> src/main.rs:9:49
9 |>             panic!("Error description: {}", err.description());
  |>                                                 ^^^^^^^^^^^
note: in this expansion of format_args!
note: in this expansion of panic! (defined in <std macros>)
help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
help: candidate #1: `use std::error::Error`

Note the last line:

items from traits can only be used if the trait is in scope

And

the following trait is implemented but not in scope, perhaps add a use for it: use std::error::Error

Doing as the compiler suggests allows the code to compile.


I already had tried to use std::error::Error, but also had added postgres::error::Error. Having both broke it.

There are a few solutions here...

Avoid fully importing the type:

use postgres::error;
use std::error::Error;

let result: Result<_, error::Error> = Ok(());

Avoid fully importing the trait:

use postgres::error::Error;
use std::error;

panic!("Error description: {}", error::Error::description(&err));

Rename the type on import (my preferred solution):

use postgres::error::Error as PgError;
use std::error::Error;

let result: Result<_, PgError> = Ok(());

Rename the trait on import:

use postgres::error::Error;
use std::error::Error as Awesome;

Upvotes: 4

Related Questions