From 245fcc705e378e11a85ff420a7b1e27d88634dee Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Wed, 29 May 2024 22:43:29 +0300
Subject: [PATCH] chore: Create an error type for the server
Signed-off-by: Awiteb
---
Cargo.lock | 109 +++++++++++++++++++++++++++---------------------
Cargo.toml | 1 +
src/errors.rs | 46 ++++++++++++++++++++
src/main.rs | 2 +
src/superbot.rs | 10 +++--
5 files changed, 117 insertions(+), 51 deletions(-)
create mode 100644 src/errors.rs
diff --git a/Cargo.lock b/Cargo.lock
index 51e256d..0ec3be1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4,9 +4,9 @@ version = 3
[[package]]
name = "addr2line"
-version = "0.21.0"
+version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb"
+checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678"
dependencies = [
"gimli",
]
@@ -84,7 +84,7 @@ checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.65",
+ "syn 2.0.66",
]
[[package]]
@@ -101,9 +101,9 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]]
name = "backtrace"
-version = "0.3.71"
+version = "0.3.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d"
+checksum = "17c6a35df3749d2e8bb1b7b21a976d82b15548788d2735b9d82f329268f71a11"
dependencies = [
"addr2line",
"cc",
@@ -377,7 +377,7 @@ checksum = "5c785274071b1b420972453b306eeca06acf4633829db4223b58a2a8c5953bc4"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.65",
+ "syn 2.0.66",
]
[[package]]
@@ -515,7 +515,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.65",
+ "syn 2.0.66",
]
[[package]]
@@ -582,9 +582,9 @@ dependencies = [
[[package]]
name = "gimli"
-version = "0.28.1"
+version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
+checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd"
[[package]]
name = "glass_pumpkin"
@@ -857,9 +857,9 @@ dependencies = [
[[package]]
name = "hyper-rustls"
-version = "0.27.1"
+version = "0.27.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "908bb38696d7a037a01ebcc68a00634112ac2bbf8ca74e30a2c3d2f4f021302b"
+checksum = "5ee4be2c948921a1a5320b629c4193916ed787a7f7f293fd3f7f5a6c9de74155"
dependencies = [
"futures-util",
"http",
@@ -892,9 +892,9 @@ dependencies = [
[[package]]
name = "hyper-util"
-version = "0.1.4"
+version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3d8d52be92d09acc2e01dddb7fde3ad983fc6489c7db4837e605bc3fca4cb63e"
+checksum = "7b875924a60b96e5d7b9ae7b066540b1dd1cbd90d1828f54c92e02a283351c56"
dependencies = [
"bytes",
"futures-channel",
@@ -1156,12 +1156,17 @@ dependencies = [
]
[[package]]
-name = "native-tls"
-version = "0.2.11"
+name = "myn"
+version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e"
+checksum = "51eb7addae0a5fbc6616160ee79bb7244eb644e2d18becf2f8f03603e06de63a"
+
+[[package]]
+name = "native-tls"
+version = "0.2.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466"
dependencies = [
- "lazy_static",
"libc",
"log",
"openssl",
@@ -1254,9 +1259,9 @@ dependencies = [
[[package]]
name = "object"
-version = "0.32.2"
+version = "0.35.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441"
+checksum = "b8ec7ab813848ba4522158d5517a6093db1ded27575b070f4177b8d12b41db5e"
dependencies = [
"memchr",
]
@@ -1267,6 +1272,15 @@ version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
+[[package]]
+name = "onlyerror"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8c26d4ea2ccd9b7acedc478853805606e60c5b7b7aedfc1c54ed6d1fdc0587db"
+dependencies = [
+ "myn",
+]
+
[[package]]
name = "opaque-debug"
version = "0.3.1"
@@ -1296,7 +1310,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.65",
+ "syn 2.0.66",
]
[[package]]
@@ -1319,19 +1333,19 @@ dependencies = [
[[package]]
name = "os_info"
-version = "3.7.0"
+version = "3.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "006e42d5b888366f1880eda20371fedde764ed2213dc8496f49622fa0c99cd5e"
+checksum = "ae99c7fa6dd38c7cafe1ec085e804f8f555a2f8659b0dbe03f1f9963a9b51092"
dependencies = [
"log",
- "winapi",
+ "windows-sys 0.52.0",
]
[[package]]
name = "parking_lot"
-version = "0.12.2"
+version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7e4af0ca4f6caed20e900d564c242b8e5d4903fdacf31d3daf527b66fe6f42fb"
+checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27"
dependencies = [
"lock_api",
"parking_lot_core",
@@ -1393,7 +1407,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.65",
+ "syn 2.0.66",
]
[[package]]
@@ -1483,9 +1497,9 @@ dependencies = [
[[package]]
name = "proc-macro2"
-version = "1.0.83"
+version = "1.0.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0b33eb56c327dec362a9e55b3ad14f9d2f0904fb5a5b03b513ab5465399e9f43"
+checksum = "ec96c6a92621310b51366f1e28d05ef11489516e93be030060e5fc12024a49d6"
dependencies = [
"unicode-ident",
]
@@ -1819,7 +1833,7 @@ dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
- "syn 2.0.65",
+ "syn 2.0.66",
]
[[package]]
@@ -1899,7 +1913,7 @@ dependencies = [
"quote",
"regex",
"salvo-serde-util",
- "syn 2.0.65",
+ "syn 2.0.66",
]
[[package]]
@@ -1919,11 +1933,11 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "security-framework"
-version = "2.10.0"
+version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "770452e37cad93e0a50d5abc3990d2bc351c36d0328f86cefec2f2fb206eaef6"
+checksum = "c627723fd09706bacdb5cf41499e95098555af3c3c29d014dc3c458ef6be11c0"
dependencies = [
- "bitflags 1.3.2",
+ "bitflags 2.5.0",
"core-foundation",
"core-foundation-sys",
"libc",
@@ -1932,9 +1946,9 @@ dependencies = [
[[package]]
name = "security-framework-sys"
-version = "2.10.0"
+version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "41f3cc463c0ef97e11c3461a9d3787412d30e8e7eb907c79180c4a57bf7c04ef"
+checksum = "317936bbbd05227752583946b9e66d7ce3b489f84e11a94a510b4437fef407d7"
dependencies = [
"core-foundation-sys",
"libc",
@@ -1942,9 +1956,9 @@ dependencies = [
[[package]]
name = "serde"
-version = "1.0.202"
+version = "1.0.203"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395"
+checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094"
dependencies = [
"serde_derive",
]
@@ -1963,13 +1977,13 @@ dependencies = [
[[package]]
name = "serde_derive"
-version = "1.0.202"
+version = "1.0.203"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838"
+checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.65",
+ "syn 2.0.66",
]
[[package]]
@@ -2112,9 +2126,9 @@ dependencies = [
[[package]]
name = "syn"
-version = "2.0.65"
+version = "2.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d2863d96a84c6439701d7a38f9de935ec562c8832cc55d1dde0f513b52fad106"
+checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5"
dependencies = [
"proc-macro2",
"quote",
@@ -2164,6 +2178,7 @@ dependencies = [
"grammers-session",
"lazy_static",
"log",
+ "onlyerror",
"pretty_env_logger",
"promptly",
"salvo",
@@ -2211,7 +2226,7 @@ checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.65",
+ "syn 2.0.66",
]
[[package]]
@@ -2286,7 +2301,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.65",
+ "syn 2.0.66",
]
[[package]]
@@ -2398,7 +2413,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.65",
+ "syn 2.0.66",
]
[[package]]
@@ -2576,7 +2591,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
- "syn 2.0.65",
+ "syn 2.0.66",
"wasm-bindgen-shared",
]
@@ -2610,7 +2625,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [
"proc-macro2",
"quote",
- "syn 2.0.65",
+ "syn 2.0.66",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
diff --git a/Cargo.toml b/Cargo.toml
index 279f5e4..43f075d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -18,6 +18,7 @@ grammers-client = "0.6.0"
grammers-session = "0.5.2"
lazy_static = "1.4.0"
log = "0.4.21"
+onlyerror = "0.1.4"
pretty_env_logger = "0.5.0"
promptly = "0.3.1"
salvo = {version = "0.67.2", features = ["logging", "affix", "rustls"]}
diff --git a/src/errors.rs b/src/errors.rs
new file mode 100644
index 0000000..7debbbf
--- /dev/null
+++ b/src/errors.rs
@@ -0,0 +1,46 @@
+// A simple API to ping telegram bots and returns if it's online or not.
+// Copyright (C) 2023-2024 Awiteb
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see .
+
+use std::io::Error as IoError;
+
+use grammers_client::client::bots::{AuthorizationError, InvocationError};
+use tokio::task::JoinError;
+
+/// The result type of the server
+#[allow(clippy::absolute_paths)]
+pub(crate) type Result = std::result::Result;
+
+/// The server error
+#[derive(Debug, onlyerror::Error)]
+pub(crate) enum Error {
+ #[error("Cli Error: {0}")]
+ CliParse(String),
+ #[error("IO Error: {0}")]
+ Io(#[from] IoError),
+ #[error("Thread Error: {0}")]
+ Thread(#[from] JoinError),
+ #[error("Readline Error: {0}")]
+ Readline(#[from] promptly::ReadlineError),
+ #[error("Telegram Error: {0}")]
+ Telegram(#[from] InvocationError),
+ #[error("Error: Faild to authorize with telegram")]
+ TelegramAuth(#[from] AuthorizationError),
+ #[error("Error: Falid to signin to telegram")]
+ TelegramSignin(#[from] grammers_client::SignInError),
+
+ #[error("Error: {0}")]
+ Other(String),
+}
diff --git a/src/main.rs b/src/main.rs
index d3008e6..ac5902c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -22,9 +22,11 @@ use lazy_static::lazy_static;
use salvo::{conn::TcpListener, Listener};
mod api;
+mod errors;
mod superbot;
mod traits;
+pub(crate) use errors::{Error as ServerError, Result as ServerResult};
pub(crate) use traits::PingList;
#[derive(Default, Clone)]
diff --git a/src/superbot.rs b/src/superbot.rs
index 1225991..7cf10ad 100644
--- a/src/superbot.rs
+++ b/src/superbot.rs
@@ -18,11 +18,11 @@ use grammers_client::{Client, Config, InitParams, SignInError, Update};
use grammers_session::Session;
use tokio::{signal, time};
-use crate::PingList;
+use crate::{PingList, ServerError, ServerResult};
const SESSION_FILE: &str = "telebotping.session";
-pub(crate) async fn login(api_hash: String, api_id: i32) -> crate::Result<(Client, bool)> {
+pub(crate) async fn login(api_hash: String, api_id: i32) -> ServerResult<(Client, bool)> {
let client = Client::connect(Config {
session: Session::load_file_or_create(SESSION_FILE)?,
api_id,
@@ -96,7 +96,7 @@ pub(crate) async fn handler(client: Client) {
}
}
-pub(crate) async fn send_start(client: &Client, bot_username: &str) -> crate::Result {
+pub(crate) async fn send_start(client: &Client, bot_username: &str) -> ServerResult {
if let Some(chat) = client.resolve_username(bot_username).await? {
let telegram_id = chat.id() as u64;
crate::PINGED_BOTS.add_new(telegram_id);
@@ -105,6 +105,8 @@ pub(crate) async fn send_start(client: &Client, bot_username: &str) -> crate::Re
time::sleep(time::Duration::from_secs(2)).await;
Ok(telegram_id)
} else {
- Err(format!("Invalid username `{bot_username}`").into())
+ Err(ServerError::Other(format!(
+ "Invalid username `{bot_username}`"
+ )))
}
}