Skip to content

Commit

Permalink
Fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mechiru committed Apr 19, 2021
1 parent 6b55a89 commit e9b58a2
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions gouth/src/tonic.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use crate::Token;
use tonic::{metadata::MetadataValue, Interceptor, Request, Status};

macro_rules! map_err {
($res:expr) => {
$res.map_err(|e| Status::unknown(e.to_string()))
};
}

pub fn interceptor() -> impl Into<Interceptor> {
let token = Token::new().expect("Token::new()");
move |mut req: Request<()>| {
let token = &*token
.header_value()
.map_err(|e| Status::unknown(e.to_string()))?;
let meta = MetadataValue::from_str(token).unwrap();
let token = map_err!(token.header_value())?;
let meta = map_err!(MetadataValue::from_str(&*token))?;
req.metadata_mut().insert("authorization", meta);
Ok(req)
}
Expand Down

0 comments on commit e9b58a2

Please sign in to comment.