From a15eda92c84834fbbda802c09dff36c60a334efb Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Mon, 27 Feb 2023 13:35:49 +0200 Subject: [PATCH] feat(shared): impl most previous api errors Signed-off-by: Jonni Liljamo --- Cargo.lock | 9 +++---- shared/Cargo.toml | 2 ++ shared/src/error/api.rs | 52 +++++++++++++++++++++++++++++++++++++++++ shared/src/error/mod.rs | 9 +++++++ shared/src/lib.rs | 1 + 5 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 shared/src/error/api.rs create mode 100644 shared/src/error/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 8526867..de0c9dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5361,6 +5361,7 @@ name = "shared" version = "0.1.0" dependencies = [ "naia-bevy-shared", + "thiserror", ] [[package]] @@ -5659,18 +5660,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.37" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e" +checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.37" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb" +checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" dependencies = [ "proc-macro2", "quote", diff --git a/shared/Cargo.toml b/shared/Cargo.toml index f728be4..fb600e1 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -11,3 +11,5 @@ publish = false [dependencies] naia-bevy-shared = "0.16.0" + +thiserror = "1.0.38" diff --git a/shared/src/error/api.rs b/shared/src/error/api.rs new file mode 100644 index 0000000..536a681 --- /dev/null +++ b/shared/src/error/api.rs @@ -0,0 +1,52 @@ +/* + * This file is part of laurelin/shared + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use thiserror::Error; + +#[doc = "API errors are in the 1XXXX range"] +#[derive(Debug, Error)] +pub enum APIError { + // general errors + #[doc = "Should never be used, but still exists just in case"] + #[error("undefined error")] + Undefined = 10000, + + #[doc = "Authorization doesn't meet requirements"] + #[error("not authorized")] + NotAuthorized = 10001, + + #[doc = "Authorization is missing from the request"] + #[error("missing authorization")] + MissingAuthorization = 10002, + + // user related errors + #[doc = "User does not exist in the database"] + #[error("user not found")] + UserNotFound = 11000, + + #[doc = "Invalid credentials were passed"] + #[error("invalid credentials")] + UserInvalidCredentials = 11001, + + // login errors + #[doc = ""] + #[error("invalid email")] + UserEmailInvalid = 11100, + + #[doc = ""] + #[error("username should not be shorter than 3 characters")] + UserUsernameTooShort = 11101, + + #[doc = ""] + #[error("password should not be shorter than 8 characters")] + UserPasswordTooShort = 11102, + + #[doc = ""] + #[error("user creation failed")] + UserCreationFailed = 11103, +} diff --git a/shared/src/error/mod.rs b/shared/src/error/mod.rs new file mode 100644 index 0000000..9b5e843 --- /dev/null +++ b/shared/src/error/mod.rs @@ -0,0 +1,9 @@ +/* + * This file is part of laurelin/shared + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +mod api; diff --git a/shared/src/lib.rs b/shared/src/lib.rs index 668d51f..7f637e1 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -6,4 +6,5 @@ * See LICENSE for licensing information. */ +pub mod error; pub mod server; -- 2.44.1