From c6ded565103dc68056a3adb83ebeb306056a7e76 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 22 Apr 2023 11:32:44 +0200 Subject: [PATCH] push progress --- src/api/auth/register.rs | 29 ++++++++++++++++++++--------- src/api/schemas.rs | 18 ++++++++++++++++++ src/errors.rs | 5 +++-- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/api/auth/register.rs b/src/api/auth/register.rs index 943a5c9..3c7d3e7 100644 --- a/src/api/auth/register.rs +++ b/src/api/auth/register.rs @@ -1,9 +1,12 @@ pub mod register { use reqwest::Client; - use serde_json::json; + use serde_json::{from_str, json}; use crate::{ - api::{limits::LimitType, schemas::schemas::RegisterSchema}, + api::{ + limits::LimitType, + schemas::schemas::{ErrorBody, RegisterSchema}, + }, errors::InstanceServerError, instance::{Instance, Token}, }; @@ -30,16 +33,24 @@ pub mod register { .await; if response.is_none() { return Err(InstanceServerError::NoResponse); - } else { - // temp - return Err(InstanceServerError::NoResponse); - } // end temp + } + + let response_unwrap = response.unwrap(); + let status = response_unwrap.status(); + let response_text_string = response_unwrap.text().await.unwrap(); + if status.is_client_error() { + let error: ErrorBody = from_str(&response_text_string).unwrap(); + return Err(InstanceServerError::InvalidFormBodyError { + error: error.errors.errors.iter().next().unwrap().code.clone(), + }); + } + return Ok(Token { + token: response_text_string, + }); /* Things to do: - 1. Check the response for Errors. If the Response says the request is missing a field, - return an Err() that says that. - + Check out the serde error. Maybe make a seperate project to find out how flatten works */ } } diff --git a/src/api/schemas.rs b/src/api/schemas.rs index d2a7ccc..2c53d83 100644 --- a/src/api/schemas.rs +++ b/src/api/schemas.rs @@ -150,6 +150,24 @@ pub mod schemas { ) } } + + #[derive(Debug, Serialize, Deserialize)] + pub struct ErrorBody { + code: i32, + pub message: String, + pub errors: ErrorObject, + } + + #[derive(Debug, Serialize, Deserialize)] + pub struct ErrorObject { + #[serde(flatten)] + pub errors: Vec, + } + #[derive(Debug, Serialize, Deserialize)] + pub struct Error { + pub message: String, + pub code: String, + } } // I know that some of these tests are... really really basic and unneccessary, but sometimes, I diff --git a/src/errors.rs b/src/errors.rs index 0b54704..4a63d09 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -6,7 +6,7 @@ custom_error! { PasswordError = "Password must be between 1 and 72 characters.", UsernameError = "Username must be between 2 and 32 characters.", ConsentError = "Consent must be 'true' to register.", - EmailError = "The provided email address is in an invalid format." + EmailError = "The provided email address is in an invalid format.", } custom_error! { @@ -15,5 +15,6 @@ custom_error! { NoResponse = "Did not receive a response from the Server.", RequestErrorError{url:String, error:String} = "An error occured while trying to GET from {url}: {error}", ReceivedErrorCodeError{error_code:String} = "Received the following error code while requesting from the route: {error_code}", - CantGetInfoError{error:String} = "Something seems to be wrong with the instance. Cannot get information about the instance: {error}" + CantGetInfoError{error:String} = "Something seems to be wrong with the instance. Cannot get information about the instance: {error}", + InvalidFormBodyError{error:String} = "The server responded with: {error}", }