errors: use anyhow::Context everywhere

This commit is contained in:
Valentin Tolmer
2021-08-26 21:56:42 +02:00
committed by nitnelave
parent a08b9a556d
commit 83ed58bff2
5 changed files with 23 additions and 27 deletions

View File

@@ -1,5 +1,5 @@
use crate::cookies::set_cookie;
use anyhow::{anyhow, Result};
use anyhow::{anyhow, Context, Result};
use lldap_model::*;
use yew::callback::Callback;
@@ -33,7 +33,7 @@ where
Callback::once(move |response: Response<Result<Resp>>| {
let (meta, maybe_data) = response.into_parts();
let message = maybe_data
.map_err(|e| anyhow!("Could not reach server: {}", e))
.context("Could not reach server")
.and_then(|data| handler(meta.status, data));
callback.emit(message)
})
@@ -100,7 +100,7 @@ where
callback,
move |status: http::StatusCode, data: String| {
if status.is_success() {
serde_json::from_str(&data).map_err(|e| anyhow!("Could not parse response: {}", e))
serde_json::from_str(&data).context("Could not parse response")
} else {
Err(anyhow!("{}[{}]: {}", error_message, status, data))
}
@@ -148,7 +148,7 @@ impl HostService {
|status, data: String| {
if status.is_success() {
get_claims_from_jwt(&data)
.map_err(|e| anyhow!("Could not parse response: {}", e))
.context("Could not parse response")
.and_then(|jwt_claims| {
let is_admin = jwt_claims.groups.contains("lldap_admin");
set_cookie("user_id", &jwt_claims.user, &jwt_claims.exp)
@@ -156,7 +156,7 @@ impl HostService {
set_cookie("is_admin", &is_admin.to_string(), &jwt_claims.exp)
})
.map(|_| (jwt_claims.user.clone(), is_admin))
.map_err(|e| anyhow!("Error clearing cookie: {}", e))
.context("Error clearing cookie")
})
} else {
Err(anyhow!(

View File

@@ -1,5 +1,5 @@
use crate::api::HostService;
use anyhow::{anyhow, Result};
use anyhow::{anyhow, Context, Result};
use lldap_model::*;
use yew::prelude::*;
use yew::services::{fetch::FetchTask, ConsoleService};
@@ -48,7 +48,7 @@ impl CreateUserForm {
};
self._task = Some(
HostService::create_user(req, self.link.callback(Msg::CreateUserResponse))
.map_err(|e| anyhow!("Error trying to create user: {}", e))?,
.context("Error trying to create user")?,
);
}
Msg::CreateUserResponse(r) => {
@@ -73,7 +73,7 @@ impl CreateUserForm {
req,
self.link.callback(Msg::RegistrationStartResponse),
)
.map_err(|e| anyhow!("Error trying to create user: {}", e))?,
.context("Error trying to create user")?,
);
} else {
self.update(Msg::SuccessfulCreation);
@@ -97,7 +97,7 @@ impl CreateUserForm {
req,
self.link.callback(Msg::RegistrationFinishResponse),
)
.map_err(|e| anyhow!("Error trying to register user: {}", e))?,
.context("Error trying to register user")?,
);
}
Msg::RegistrationFinishResponse(response) => {

View File

@@ -1,5 +1,5 @@
use crate::api::HostService;
use anyhow::{anyhow, Result};
use anyhow::{anyhow, Context, Result};
use lldap_model::*;
use wasm_bindgen::JsCast;
use yew::prelude::*;
@@ -61,9 +61,8 @@ impl LoginForm {
let password = get_form_field("password")
.ok_or_else(|| anyhow!("Could not get password from form"))?;
let mut rng = rand::rngs::OsRng;
let login_start_request =
opaque::client::login::start_login(&password, &mut rng)
.map_err(|e| anyhow!("Could not initialize login: {}", e))?;
let login_start_request = opaque::client::login::start_login(&password, &mut rng)
.context("Could not initialize login")?;
self.login_start = Some(login_start_request.state);
let req = login::ClientLoginStartRequest {
username,