Move custom errros to central file

This commit is contained in:
bitfl0wer 2023-04-21 16:18:20 +02:00
parent 552b43f80b
commit f96bcb87f6
No known key found for this signature in database
GPG Key ID: 84BBB60DF895ABF2
4 changed files with 22 additions and 17 deletions

View File

@ -1,17 +1,10 @@
pub mod instance {
use custom_error::custom_error;
use reqwest::Client;
use serde_json::from_str;
use crate::errors::InstancePoliciesError;
use crate::{api::schemas::schemas::InstancePoliciesSchema, instance::Instance};
custom_error! {
#[derive(PartialEq, Eq)]
pub InstancePoliciesError
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}"
}
impl Instance {
/**
Gets the instance policies schema.

View File

@ -5,6 +5,8 @@ pub mod schemas {
use regex::Regex;
use serde::{Deserialize, Serialize};
use crate::errors::RegisterSchemaError;
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct RegisterSchema {
@ -20,15 +22,6 @@ pub mod schemas {
promotional_email_opt_in: Option<bool>,
}
custom_error! {
#[derive(PartialEq, Eq)]
pub RegisterSchemaError
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."
}
impl RegisterSchema {
/**
Returns a new [`Result<RegisterSchema, RegisterSchemaError>`].
@ -166,6 +159,7 @@ pub mod schemas {
#[cfg(test)]
mod schemas_tests {
use super::schemas::*;
use crate::errors::RegisterSchemaError;
#[test]
fn password_too_short() {

17
src/errors.rs Normal file
View File

@ -0,0 +1,17 @@
use custom_error::custom_error;
custom_error! {
#[derive(PartialEq, Eq)]
pub InstancePoliciesError
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}"
}
custom_error! {
#[derive(PartialEq, Eq)]
pub RegisterSchemaError
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."
}

View File

@ -1,4 +1,5 @@
mod api;
mod errors;
mod gateway;
mod instance;
mod limit;