DEVELOPMENT ENVIRONMENT

~liljamo/emerwen

a10abbc6c142abb0bda216e49669ac8baa29df99 — Jonni Liljamo 12 days ago 5d06034
fix(master): get_targets error handling
1 files changed, 25 insertions(+), 14 deletions(-)

M emerwen-master/src/main.rs
M emerwen-master/src/main.rs => emerwen-master/src/main.rs +25 -14
@@ 107,23 107,34 @@ impl MasterServer {
#[tonic::async_trait]
impl EmerwenProtocol for MasterServer {
    async fn get_targets(&self, request: Request<()>) -> Result<Response<TargetsResponse>, Status> {
        // We should not get here without valid authorization.
        let token = request
            .metadata()
            .get("authorization")
            .expect("request had no authorization, when it should")
            .to_str()
            .unwrap()
            .trim_start_matches("Bearer ");

        // Like we really really shouldn't get here if the target doesn't exist.
        let targets = self
        let token = match request.metadata().get("authorization") {
            Some(value) => match value.to_str() {
                Ok(bearer_token) => bearer_token.trim_start_matches("Bearer "),
                Err(_) => {
                    return Err(Status::invalid_argument(
                        "couldn't read bearer auth to string",
                    ))
                }
            },
            None => {
                return Err(Status::unauthenticated(
                    "request had no authorization, when it should",
                ))
            }
        };

        let targets = match self
            .workers
            .iter()
            .find(|worker| *worker.auth_token == *token)
            .expect("dafuq")
            .targets
            .clone();
        {
            Some(worker) => worker.targets.clone(),
            None => {
                return Err(Status::invalid_argument(
                    "no worker exists with the auth token",
                ))
            }
        };

        Ok(Response::new(TargetsResponse { targets }))
    }