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 sodiumoxide::crypto::secretbox;
use atuin_common::api::{ use atuin_common::api::{
AddHistoryRequest, CountResponse, LoginRequest, LoginResponse, RegisterResponse, AddHistoryRequest, CountResponse, ErrorResponse, LoginRequest, LoginResponse, RegisterResponse,
SyncHistoryResponse, SyncHistoryResponse,
}; };
@ -58,7 +58,8 @@ pub async fn register(
.await?; .await?;
if !resp.status().is_success() { 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?; let session = resp.json::<RegisterResponse>().await?;
@ -77,7 +78,8 @@ pub async fn login(address: &str, req: LoginRequest) -> Result<LoginResponse> {
.await?; .await?;
if resp.status() != reqwest::StatusCode::OK { 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?; let session = resp.json::<LoginResponse>().await?;

View File

@ -1,5 +1,6 @@
use chrono::Utc; use chrono::Utc;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::borrow::Cow;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct UserResponse { pub struct UserResponse {
@ -53,3 +54,8 @@ pub struct SyncHistoryRequest {
pub struct SyncHistoryResponse { pub struct SyncHistoryResponse {
pub history: Vec<String>, 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 http::StatusCode;
use tracing::{debug, error, instrument}; use tracing::{debug, error, instrument};
use super::{ErrorResponse, ErrorResponseStatus}; use super::{ErrorResponse, ErrorResponseStatus, RespExt};
use crate::{ use crate::{
calendar::{TimePeriod, TimePeriodInfo}, calendar::{TimePeriod, TimePeriodInfo},
database::{Database, Postgres}, database::{Database, Postgres},

View File

@ -1,5 +1,4 @@
use std::borrow::Cow; use atuin_common::api::ErrorResponse;
use axum::{response::IntoResponse, Json}; use axum::{response::IntoResponse, Json};
use serde::{Deserialize, Serialize}; 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> { impl<'a> IntoResponse for ErrorResponseStatus<'a> {
fn into_response(self) -> axum::response::Response { fn into_response(self) -> axum::response::Response {
(self.status, Json(self.error)).into_response() (self.status, Json(self.error)).into_response()
@ -39,15 +33,20 @@ pub struct ErrorResponseStatus<'a> {
pub status: http::StatusCode, pub status: http::StatusCode,
} }
impl<'a> ErrorResponse<'a> { pub trait RespExt<'a> {
pub fn with_status(self, status: http::StatusCode) -> ErrorResponseStatus<'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 { ErrorResponseStatus {
error: self, error: self,
status, status,
} }
} }
pub fn reply(reason: &'a str) -> ErrorResponse { fn reply(reason: &'a str) -> ErrorResponse {
Self { Self {
reason: reason.into(), reason: reason.into(),
} }

View File

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