Some more error handling

This commit is contained in:
2021-05-20 20:00:14 +02:00
parent 813660c022
commit b77c863065
6 changed files with 161 additions and 59 deletions

View File

@ -6,8 +6,8 @@ use crate::error::*;
pub fn endpoint(endpoint: &str, input: serde_json::Value) -> Result<serde_json::Value, Error> {
match endpoint {
"echo" => Ok(serde_json::to_value(echo(serde_json::from_value(input)?))?),
_ => Err(Error::new(ErrorKind::InvalidEndpointError)),
"echo" => Ok(serde_json::to_value(echo(serde_json::from_value(input)?)?)?),
_ => Err(Error::new(Kind::InvalidEndpointError, Class::ClientError)),
}
}
@ -22,19 +22,19 @@ pub struct EchoOutput {
database: Option<i32>,
}
pub fn echo(input: EchoInput) -> EchoOutput {
let backend = database::client();
pub fn echo(input: EchoInput) -> Result<EchoOutput, Error> {
let backend = database::client()?;
let mut output = EchoOutput {
message: input.message,
database: None,
};
match backend {
database::Client::Postgres(mut client) => {
let res = client.query("SELECT * FROM test", &[]).unwrap();
let res = client.query("SELECT * FROM test", &[])?;
for row in res {
output.database = Some(row.get(0));
}
}
}
output
Ok(output)
}