chore: Create an error type for the server

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-05-29 22:43:29 +03:00
parent 53f58f25ee
commit 245fcc705e
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
5 changed files with 117 additions and 51 deletions

109
Cargo.lock generated
View file

@ -4,9 +4,9 @@ version = 3
[[package]] [[package]]
name = "addr2line" name = "addr2line"
version = "0.21.0" version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678"
dependencies = [ dependencies = [
"gimli", "gimli",
] ]
@ -84,7 +84,7 @@ checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.65", "syn 2.0.66",
] ]
[[package]] [[package]]
@ -101,9 +101,9 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]] [[package]]
name = "backtrace" name = "backtrace"
version = "0.3.71" version = "0.3.72"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" checksum = "17c6a35df3749d2e8bb1b7b21a976d82b15548788d2735b9d82f329268f71a11"
dependencies = [ dependencies = [
"addr2line", "addr2line",
"cc", "cc",
@ -377,7 +377,7 @@ checksum = "5c785274071b1b420972453b306eeca06acf4633829db4223b58a2a8c5953bc4"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.65", "syn 2.0.66",
] ]
[[package]] [[package]]
@ -515,7 +515,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.65", "syn 2.0.66",
] ]
[[package]] [[package]]
@ -582,9 +582,9 @@ dependencies = [
[[package]] [[package]]
name = "gimli" name = "gimli"
version = "0.28.1" version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd"
[[package]] [[package]]
name = "glass_pumpkin" name = "glass_pumpkin"
@ -857,9 +857,9 @@ dependencies = [
[[package]] [[package]]
name = "hyper-rustls" name = "hyper-rustls"
version = "0.27.1" version = "0.27.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "908bb38696d7a037a01ebcc68a00634112ac2bbf8ca74e30a2c3d2f4f021302b" checksum = "5ee4be2c948921a1a5320b629c4193916ed787a7f7f293fd3f7f5a6c9de74155"
dependencies = [ dependencies = [
"futures-util", "futures-util",
"http", "http",
@ -892,9 +892,9 @@ dependencies = [
[[package]] [[package]]
name = "hyper-util" name = "hyper-util"
version = "0.1.4" version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d8d52be92d09acc2e01dddb7fde3ad983fc6489c7db4837e605bc3fca4cb63e" checksum = "7b875924a60b96e5d7b9ae7b066540b1dd1cbd90d1828f54c92e02a283351c56"
dependencies = [ dependencies = [
"bytes", "bytes",
"futures-channel", "futures-channel",
@ -1156,12 +1156,17 @@ dependencies = [
] ]
[[package]] [[package]]
name = "native-tls" name = "myn"
version = "0.2.11" version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" 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 = [ dependencies = [
"lazy_static",
"libc", "libc",
"log", "log",
"openssl", "openssl",
@ -1254,9 +1259,9 @@ dependencies = [
[[package]] [[package]]
name = "object" name = "object"
version = "0.32.2" version = "0.35.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" checksum = "b8ec7ab813848ba4522158d5517a6093db1ded27575b070f4177b8d12b41db5e"
dependencies = [ dependencies = [
"memchr", "memchr",
] ]
@ -1267,6 +1272,15 @@ version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "onlyerror"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c26d4ea2ccd9b7acedc478853805606e60c5b7b7aedfc1c54ed6d1fdc0587db"
dependencies = [
"myn",
]
[[package]] [[package]]
name = "opaque-debug" name = "opaque-debug"
version = "0.3.1" version = "0.3.1"
@ -1296,7 +1310,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.65", "syn 2.0.66",
] ]
[[package]] [[package]]
@ -1319,19 +1333,19 @@ dependencies = [
[[package]] [[package]]
name = "os_info" name = "os_info"
version = "3.7.0" version = "3.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "006e42d5b888366f1880eda20371fedde764ed2213dc8496f49622fa0c99cd5e" checksum = "ae99c7fa6dd38c7cafe1ec085e804f8f555a2f8659b0dbe03f1f9963a9b51092"
dependencies = [ dependencies = [
"log", "log",
"winapi", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.12.2" version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e4af0ca4f6caed20e900d564c242b8e5d4903fdacf31d3daf527b66fe6f42fb" checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27"
dependencies = [ dependencies = [
"lock_api", "lock_api",
"parking_lot_core", "parking_lot_core",
@ -1393,7 +1407,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.65", "syn 2.0.66",
] ]
[[package]] [[package]]
@ -1483,9 +1497,9 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.83" version = "1.0.84"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b33eb56c327dec362a9e55b3ad14f9d2f0904fb5a5b03b513ab5465399e9f43" checksum = "ec96c6a92621310b51366f1e28d05ef11489516e93be030060e5fc12024a49d6"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
@ -1819,7 +1833,7 @@ dependencies = [
"proc-macro-error", "proc-macro-error",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.65", "syn 2.0.66",
] ]
[[package]] [[package]]
@ -1899,7 +1913,7 @@ dependencies = [
"quote", "quote",
"regex", "regex",
"salvo-serde-util", "salvo-serde-util",
"syn 2.0.65", "syn 2.0.66",
] ]
[[package]] [[package]]
@ -1919,11 +1933,11 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]] [[package]]
name = "security-framework" name = "security-framework"
version = "2.10.0" version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "770452e37cad93e0a50d5abc3990d2bc351c36d0328f86cefec2f2fb206eaef6" checksum = "c627723fd09706bacdb5cf41499e95098555af3c3c29d014dc3c458ef6be11c0"
dependencies = [ dependencies = [
"bitflags 1.3.2", "bitflags 2.5.0",
"core-foundation", "core-foundation",
"core-foundation-sys", "core-foundation-sys",
"libc", "libc",
@ -1932,9 +1946,9 @@ dependencies = [
[[package]] [[package]]
name = "security-framework-sys" name = "security-framework-sys"
version = "2.10.0" version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41f3cc463c0ef97e11c3461a9d3787412d30e8e7eb907c79180c4a57bf7c04ef" checksum = "317936bbbd05227752583946b9e66d7ce3b489f84e11a94a510b4437fef407d7"
dependencies = [ dependencies = [
"core-foundation-sys", "core-foundation-sys",
"libc", "libc",
@ -1942,9 +1956,9 @@ dependencies = [
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.202" version = "1.0.203"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395" checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
@ -1963,13 +1977,13 @@ dependencies = [
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.202" version = "1.0.203"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838" checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.65", "syn 2.0.66",
] ]
[[package]] [[package]]
@ -2112,9 +2126,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.65" version = "2.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2863d96a84c6439701d7a38f9de935ec562c8832cc55d1dde0f513b52fad106" checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -2164,6 +2178,7 @@ dependencies = [
"grammers-session", "grammers-session",
"lazy_static", "lazy_static",
"log", "log",
"onlyerror",
"pretty_env_logger", "pretty_env_logger",
"promptly", "promptly",
"salvo", "salvo",
@ -2211,7 +2226,7 @@ checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.65", "syn 2.0.66",
] ]
[[package]] [[package]]
@ -2286,7 +2301,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.65", "syn 2.0.66",
] ]
[[package]] [[package]]
@ -2398,7 +2413,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.65", "syn 2.0.66",
] ]
[[package]] [[package]]
@ -2576,7 +2591,7 @@ dependencies = [
"once_cell", "once_cell",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.65", "syn 2.0.66",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -2610,7 +2625,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.65", "syn 2.0.66",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]

View file

@ -18,6 +18,7 @@ grammers-client = "0.6.0"
grammers-session = "0.5.2" grammers-session = "0.5.2"
lazy_static = "1.4.0" lazy_static = "1.4.0"
log = "0.4.21" log = "0.4.21"
onlyerror = "0.1.4"
pretty_env_logger = "0.5.0" pretty_env_logger = "0.5.0"
promptly = "0.3.1" promptly = "0.3.1"
salvo = {version = "0.67.2", features = ["logging", "affix", "rustls"]} salvo = {version = "0.67.2", features = ["logging", "affix", "rustls"]}

46
src/errors.rs Normal file
View file

@ -0,0 +1,46 @@
// A simple API to ping telegram bots and returns if it's online or not.
// Copyright (C) 2023-2024 Awiteb <a@4rs.nl>
//
// 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 <https://www.gnu.org/licenses/agpl-3.0>.
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<T> = std::result::Result<T, Error>;
/// 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),
}

View file

@ -22,9 +22,11 @@ use lazy_static::lazy_static;
use salvo::{conn::TcpListener, Listener}; use salvo::{conn::TcpListener, Listener};
mod api; mod api;
mod errors;
mod superbot; mod superbot;
mod traits; mod traits;
pub(crate) use errors::{Error as ServerError, Result as ServerResult};
pub(crate) use traits::PingList; pub(crate) use traits::PingList;
#[derive(Default, Clone)] #[derive(Default, Clone)]

View file

@ -18,11 +18,11 @@ use grammers_client::{Client, Config, InitParams, SignInError, Update};
use grammers_session::Session; use grammers_session::Session;
use tokio::{signal, time}; use tokio::{signal, time};
use crate::PingList; use crate::{PingList, ServerError, ServerResult};
const SESSION_FILE: &str = "telebotping.session"; 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 { let client = Client::connect(Config {
session: Session::load_file_or_create(SESSION_FILE)?, session: Session::load_file_or_create(SESSION_FILE)?,
api_id, 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<u64> { pub(crate) async fn send_start(client: &Client, bot_username: &str) -> ServerResult<u64> {
if let Some(chat) = client.resolve_username(bot_username).await? { if let Some(chat) = client.resolve_username(bot_username).await? {
let telegram_id = chat.id() as u64; let telegram_id = chat.id() as u64;
crate::PINGED_BOTS.add_new(telegram_id); 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; time::sleep(time::Duration::from_secs(2)).await;
Ok(telegram_id) Ok(telegram_id)
} else { } else {
Err(format!("Invalid username `{bot_username}`").into()) Err(ServerError::Other(format!(
"Invalid username `{bot_username}`"
)))
} }
} }