add some error messages (#510)

* add some error messages

* fmt
This commit is contained in:
Conrad Ludgate 2022-10-08 04:33:07 +01:00 committed by GitHub
parent 0f77f8ae72
commit 5725f4b40b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 15 deletions

View file

@ -9,7 +9,7 @@ use reqwest::{
use sodiumoxide::crypto::secretbox;
use atuin_common::api::{
AddHistoryRequest, CountResponse, LoginRequest, LoginResponse, RegisterResponse,
AddHistoryRequest, CountResponse, ErrorResponse, LoginRequest, LoginResponse, RegisterResponse,
SyncHistoryResponse,
};
@ -58,7 +58,8 @@ pub async fn register(
.await?;
if !resp.status().is_success() {
bail!("failed to register user");
let error = resp.json::<ErrorResponse>().await?;
bail!("failed to register user: {}", error.reason);
}
let session = resp.json::<RegisterResponse>().await?;
@ -77,7 +78,8 @@ pub async fn login(address: &str, req: LoginRequest) -> Result<LoginResponse> {
.await?;
if resp.status() != reqwest::StatusCode::OK {
bail!("invalid login details");
let error = resp.json::<ErrorResponse>().await?;
bail!("invalid login details: {}", error.reason);
}
let session = resp.json::<LoginResponse>().await?;

View file

@ -1,5 +1,6 @@
use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
#[derive(Debug, Serialize, Deserialize)]
pub struct UserResponse {
@ -53,3 +54,8 @@ pub struct SyncHistoryRequest {
pub struct SyncHistoryResponse {
pub history: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ErrorResponse<'a> {
pub reason: Cow<'a, str>,
}

View file

@ -7,7 +7,7 @@ use axum::{
use http::StatusCode;
use tracing::{debug, error, instrument};
use super::{ErrorResponse, ErrorResponseStatus};
use super::{ErrorResponse, ErrorResponseStatus, RespExt};
use crate::{
calendar::{TimePeriod, TimePeriodInfo},
database::{Database, Postgres},

View file

@ -1,5 +1,4 @@
use std::borrow::Cow;
use atuin_common::api::ErrorResponse;
use axum::{response::IntoResponse, Json};
use serde::{Deserialize, Serialize};
@ -23,11 +22,6 @@ pub async fn index() -> Json<IndexResponse> {
})
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ErrorResponse<'a> {
pub reason: Cow<'a, str>,
}
impl<'a> IntoResponse for ErrorResponseStatus<'a> {
fn into_response(self) -> axum::response::Response {
(self.status, Json(self.error)).into_response()
@ -39,15 +33,20 @@ pub struct ErrorResponseStatus<'a> {
pub status: http::StatusCode,
}
impl<'a> ErrorResponse<'a> {
pub fn with_status(self, status: http::StatusCode) -> ErrorResponseStatus<'a> {
pub trait RespExt<'a> {
fn with_status(self, status: http::StatusCode) -> ErrorResponseStatus<'a>;
fn reply(reason: &'a str) -> Self;
}
impl<'a> RespExt<'a> for ErrorResponse<'a> {
fn with_status(self, status: http::StatusCode) -> ErrorResponseStatus<'a> {
ErrorResponseStatus {
error: self,
status,
}
}
pub fn reply(reason: &'a str) -> ErrorResponse {
fn reply(reason: &'a str) -> ErrorResponse {
Self {
reason: reason.into(),
}

View file

@ -6,7 +6,7 @@ use sodiumoxide::crypto::pwhash::argon2id13;
use tracing::{debug, error, instrument};
use uuid::Uuid;
use super::{ErrorResponse, ErrorResponseStatus};
use super::{ErrorResponse, ErrorResponseStatus, RespExt};
use crate::{
database::{Database, Postgres},
models::{NewSession, NewUser},