Register function with good errors!

This commit is contained in:
bitfl0wer 2023-04-22 22:12:56 +02:00
parent c6ded56510
commit 1bdf202745
3 changed files with 67 additions and 25 deletions

View File

@ -1,11 +1,11 @@
pub mod register {
use reqwest::Client;
use serde_json::{from_str, json};
use serde_json::{from_str, json, Value};
use crate::{
api::{
limits::LimitType,
schemas::schemas::{ErrorBody, RegisterSchema},
schemas::schemas::{ErrorResponse, RegisterSchema},
},
errors::InstanceServerError,
instance::{Instance, Token},
@ -39,19 +39,19 @@ pub mod register {
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(),
});
let json: ErrorResponse = serde_json::from_str(&response_text_string).unwrap();
let error_type = json.errors.errors.iter().next().unwrap().0.to_owned();
let mut error = "".to_string();
for (_, value) in json.errors.errors.iter() {
for error_item in value._errors.iter() {
error += &(error_item.message.to_string() + " (" + &error_item.code + ")");
}
}
return Err(InstanceServerError::InvalidFormBodyError { error_type, error });
}
return Ok(Token {
token: response_text_string,
});
/*
Things to do:
Check out the serde error. Maybe make a seperate project to find out how flatten works
*/
}
}
}
@ -59,9 +59,43 @@ pub mod register {
#[cfg(test)]
mod test {
use crate::api::schemas::schemas::RegisterSchema;
use crate::errors::InstanceServerError;
use crate::instance::Instance;
use crate::limit::LimitedRequester;
use crate::URLBundle;
#[tokio::test]
async fn test_incomplete_registration() {
let urls = URLBundle::new(
"http://localhost:3001/api".to_string(),
"http://localhost:3001".to_string(),
"http://localhost:3001".to_string(),
);
let limited_requester = LimitedRequester::new(urls.get_api().to_string()).await;
let mut test_instance = Instance::new(urls.clone(), limited_requester)
.await
.unwrap();
let reg = RegisterSchema::new(
"aaa".to_string(),
None,
true,
Some("me@mail.xy".to_string()),
None,
None,
None,
None,
None,
None,
)
.unwrap();
assert_eq!(
InstanceServerError::InvalidFormBodyError {
error_type: "date_of_birth".to_string(),
error: "This field is required (BASE_TYPE_REQUIRED)".to_string()
},
test_instance.register(&reg).await.err().unwrap()
);
}
#[tokio::test]
async fn test_registration() {
let urls = URLBundle::new(
@ -74,18 +108,19 @@ mod test {
.await
.unwrap();
let reg = RegisterSchema::new(
"Test".to_string(),
None,
"Hiiii".to_string(),
Some("mysupersecurepass123!".to_string()),
true,
Some("me@mail.xy".to_string()),
None,
Some("flori@mail.xyz".to_string()),
None,
None,
Some("2000-01-01".to_string()),
None,
None,
None,
)
.unwrap();
println!("{}", test_instance.register(&reg).await.unwrap());
let token = test_instance.register(&reg).await.unwrap().token;
println!("{}", token);
}
}

View File

@ -151,19 +151,26 @@ pub mod schemas {
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ErrorBody {
code: i32,
#[derive(Serialize, Deserialize, Debug)]
pub struct ErrorResponse {
pub code: i32,
pub message: String,
pub errors: ErrorObject,
pub errors: IntermittentError,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ErrorObject {
#[derive(Serialize, Deserialize, Debug)]
pub struct IntermittentError {
#[serde(flatten)]
pub errors: Vec<Error>,
pub errors: std::collections::HashMap<String, ErrorField>,
}
#[derive(Debug, Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct ErrorField {
#[serde(default)]
pub _errors: Vec<Error>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Error {
pub message: String,
pub code: String,

View File

@ -16,5 +16,5 @@ custom_error! {
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}",
InvalidFormBodyError{error:String} = "The server responded with: {error}",
InvalidFormBodyError{error_type: String, error:String} = "The server responded with: {error_type}: {error}",
}