Factor usage of common error type more

This commit is contained in:
Dominik Werder
2021-12-08 13:20:07 +01:00
parent c39af81097
commit 3c64eafd14
56 changed files with 751 additions and 354 deletions

View File

@@ -22,7 +22,7 @@ serde_derive = "1.0"
serde_json = "1.0"
chrono = "0.4"
url = "2.2.2"
clap = "3.0.0-beta.2"
clap = "3.0.0-beta.5"
lazy_static = "1.4.0"
err = { path = "../err" }
taskrun = { path = "../taskrun" }

View File

@@ -1,5 +1,5 @@
use chrono::{DateTime, Duration, Utc};
use clap::Clap;
use clap::Parser;
use daqbuffer::cli::{ClientType, Opts, SubCmd};
use err::Error;
use netpod::log::*;
@@ -101,7 +101,7 @@ async fn go() -> Result<(), Error> {
let jh = tokio::task::spawn_blocking(move || {
taskrun::append::append(&k.dir, std::io::stdin(), std::io::stderr()).unwrap();
});
jh.await?;
jh.await.map_err(Error::from_string)?;
}
SubCmd::Test => (),
}
@@ -110,6 +110,7 @@ async fn go() -> Result<(), Error> {
#[test]
fn simple_fetch() {
use daqbuffer::err::ErrConv;
use netpod::Nanos;
use netpod::{timeunits::*, ByteOrder, Channel, ChannelConfig, ScalarType, Shape};
taskrun::run(async {
@@ -137,9 +138,10 @@ fn simple_fetch() {
let req = hyper::Request::builder()
.method(http::Method::POST)
.uri("http://localhost:8360/api/4/parsed_raw")
.body(query_string.into())?;
.body(query_string.into())
.ec()?;
let client = hyper::Client::new();
let res = client.request(req).await?;
let res = client.request(req).await.ec()?;
info!("client response {:?}", res);
let mut res_body = res.into_body();
use hyper::body::HttpBody;

View File

@@ -1,6 +1,6 @@
use clap::{crate_version, Clap};
use clap::{crate_version, Parser};
#[derive(Debug, Clap)]
#[derive(Debug, Parser)]
#[clap(name="daqbuffer", author="Dominik Werder <dominik.werder@gmail.com>", version=crate_version!())]
pub struct Opts {
#[clap(short, long, parse(from_occurrences))]
@@ -9,7 +9,7 @@ pub struct Opts {
pub subcmd: SubCmd,
}
#[derive(Debug, Clap)]
#[derive(Debug, Parser)]
pub enum SubCmd {
Retrieval(Retrieval),
Proxy(Proxy),
@@ -20,31 +20,31 @@ pub enum SubCmd {
Test,
}
#[derive(Debug, Clap)]
#[derive(Debug, Parser)]
pub struct Retrieval {
#[clap(long)]
pub config: String,
}
#[derive(Debug, Clap)]
#[derive(Debug, Parser)]
pub struct Proxy {
#[clap(long)]
pub config: String,
}
#[derive(Debug, Clap)]
#[derive(Debug, Parser)]
pub struct Client {
#[clap(subcommand)]
pub client_type: ClientType,
}
#[derive(Debug, Clap)]
#[derive(Debug, Parser)]
pub enum ClientType {
Binned(BinnedClient),
Status(StatusClient),
}
#[derive(Debug, Clap)]
#[derive(Debug, Parser)]
pub struct StatusClient {
#[clap(long)]
pub host: String,
@@ -52,7 +52,7 @@ pub struct StatusClient {
pub port: u16,
}
#[derive(Debug, Clap)]
#[derive(Debug, Parser)]
pub struct BinnedClient {
#[clap(long)]
pub host: String,
@@ -74,13 +74,13 @@ pub struct BinnedClient {
pub disk_stats_every_kb: u32,
}
#[derive(Debug, Clap)]
#[derive(Debug, Parser)]
pub struct Zmtp {
#[clap(long)]
pub addr: String,
}
#[derive(Debug, Clap)]
#[derive(Debug, Parser)]
pub struct Logappend {
#[clap(long)]
pub dir: String,

17
daqbuffer/src/err.rs Normal file
View File

@@ -0,0 +1,17 @@
pub trait ErrConv<T> {
fn ec(self) -> Result<T, ::err::Error>;
}
pub trait Convable: ToString {}
impl<T, E: Convable> ErrConv<T> for Result<T, E> {
fn ec(self) -> Result<T, err::Error> {
match self {
Ok(x) => Ok(x),
Err(e) => Err(::err::Error::from_string(e.to_string())),
}
}
}
impl Convable for http::Error {}
impl Convable for hyper::Error {}

View File

@@ -1 +1,2 @@
pub mod cli;
pub mod err;