As mentioned in the comments, there is no public way to create a postgres::Error
. You can only get one from some other postgres operation that fails. You can use the linked answer and use an Option
to create the pg_err
with None
before an error occurs, and filling it with Some(...)
when it does.
However, that is not the way you should solve this particular problem. You have a single type that is designed to express multiple different forms of responses: "ok", "no-data", and "error" as you've mentioned in the comments. So it sounds like you have RetCode
designed as such:
enum RetCode { Ok, NoData, Error }
But Rust enum
s are much more powerful than in other languages, they can hold data exclusive to a specific variant. There is a philosophy to "make invalid states unrepresentable". In other words, why have a pg_err
if no error occurred? Why have a data
field if the status is NoData
?
You can restructure your objects like so:
enum ResponseKind<T> {
Ok { data: Vec<T> },
NoData,
Error { pg_err: postgres::Error },
}
struct Response<T> {
id: i32,
desc: String,
kind: ResponseKind<T>,
}
That way you don't have to worry about creating some dummy error type.
You should heed the other comments; don't fall into the trap of simply creating a dummy constructor just for the sake of it. And new()
is just a convention which shouldn't be followed if there's no obvious default state for your type. You should have constructor functions with parameters that provide valid data to construct it with. Like:
impl<T> Response<T> {
pub fn ok(id: i32, desc: String, data: Vec<T>) -> Response<T> {
// or maybe id and desc are generated or dependent on
// kind and don't need to be parameters?
Response {
id,
desc,
kind: ResponseKind::Ok { data },
}
}
pub fn no_data(id: i32, desc: String) -> Response<T> { ... }
pub fn error(id: i32, desc: String, error: Error) -> Response<T> { ... }
}