Skip to content

Commit e13ad35

Browse files
authored
Merge pull request #23 from RAprogramm/codex/add-error-handling-for-telegram-webapp-sdk
feat: map telegram webapp sdk errors
2 parents a0ac664 + b039563 commit e13ad35

File tree

7 files changed

+201
-5
lines changed

7 files changed

+201
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44
## [Unreleased]
55
### Added
66
- `ErrorResponse::with_retry_after_duration` helper for specifying retry advice via `Duration`.
7+
- Conversion from `telegram_webapp_sdk::utils::validate_init_data::ValidationError` into `AppError` (feature `telegram-webapp-sdk`).
78

89
### Changed
910
- `AppError::log` now includes the stable `code` field alongside `kind`.

Cargo.lock

Lines changed: 125 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ config = ["dep:config"] # config::ConfigError -> AppError
2424
multipart = ["axum"]
2525
tokio = ["dep:tokio"]
2626
reqwest = ["dep:reqwest"]
27+
telegram-webapp-sdk = ["dep:telegram-webapp-sdk"]
2728
turnkey = []
2829

2930
openapi = ["dep:utoipa"]
@@ -54,6 +55,7 @@ config = { version = "0.15", optional = true }
5455
utoipa = { version = "5.3", optional = true }
5556
tokio = { version = "1", optional = true, features = ["time"] }
5657
reqwest = { version = "0.12", optional = true, default-features = false }
58+
telegram-webapp-sdk = { git = "https://github.com/RAprogramm/telegram-webapp-sdk", rev = "0c5a1d557e1cefe2b37a190c9359be05fe48d41a", optional = true }
5759

5860
[dev-dependencies]
5961
serde_json = "1"

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ utoipa = "5"
169169
<details>
170170
<summary><b>Feature flags</b></summary>
171171

172-
- `axum` — IntoResponse
173-
- `actix` — ResponseError/Responder
174-
- `openapi` — utoipa schema
175-
- `serde_json` — JSON details
176-
- `sqlx`, `redis`, `reqwest`, `validator`, `config`, `tokio`, `multipart`
172+
- `axum` — IntoResponse
173+
- `actix` — ResponseError/Responder
174+
- `openapi` — utoipa schema
175+
- `serde_json` — JSON details
176+
- `sqlx`, `redis`, `reqwest`, `validator`, `config`, `tokio`, `multipart`, `telegram-webapp-sdk`
177177
- `turnkey` — domain taxonomy and conversions for Turnkey errors
178178

179179
</details>
@@ -190,6 +190,7 @@ utoipa = "5"
190190
- `validator::ValidationErrors` → Validation
191191
- `config::ConfigError` → Config
192192
- `tokio::time::error::Elapsed` → Timeout
193+
- `telegram_webapp_sdk::utils::validate_init_data::ValidationError` → TelegramAuth
193194

194195
</details>
195196

src/convert.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ mod tokio;
113113
#[cfg_attr(docsrs, doc(cfg(feature = "validator")))]
114114
mod validator;
115115

116+
#[cfg(feature = "telegram-webapp-sdk")]
117+
#[cfg_attr(docsrs, doc(cfg(feature = "telegram-webapp-sdk")))]
118+
mod telegram_webapp_sdk;
119+
116120
/// Map `std::io::Error` to an internal application error.
117121
///
118122
/// Rationale: I/O failures are infrastructure-level and should not leak

src/convert/telegram_webapp_sdk.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//! Conversion from
2+
//! [`telegram_webapp_sdk::utils::validate_init_data::ValidationError`] into
3+
//! [`AppError`].
4+
//!
5+
//! Enabled with the `telegram-webapp-sdk` feature flag.
6+
//!
7+
//! ## Mapping
8+
//!
9+
//! All [`ValidationError`] variants are mapped to `AppErrorKind::TelegramAuth`
10+
//! and the original error text is preserved in the message.
11+
//!
12+
//! ## Rationale
13+
//!
14+
//! Failing to validate Telegram `initData` indicates an authentication problem
15+
//! with the incoming request. Mapping to `TelegramAuth` keeps this distinction
16+
//! explicit and allows callers to handle it separately from generic bad
17+
//! requests.
18+
//!
19+
//! ## Example
20+
//!
21+
//! ```rust,ignore
22+
//! use masterror::{AppError, AppErrorKind};
23+
//! use telegram_webapp_sdk::utils::validate_init_data::ValidationError;
24+
//!
25+
//! fn convert(err: ValidationError) -> AppError {
26+
//! err.into()
27+
//! }
28+
//!
29+
//! let e = convert(ValidationError::SignatureMismatch);
30+
//! assert!(matches!(e.kind, AppErrorKind::TelegramAuth));
31+
//! ```
32+
33+
#[cfg(feature = "telegram-webapp-sdk")]
34+
use telegram_webapp_sdk::utils::validate_init_data::ValidationError;
35+
36+
#[cfg(feature = "telegram-webapp-sdk")]
37+
use crate::AppError;
38+
39+
/// Map [`ValidationError`] into an [`AppError`] with kind `TelegramAuth`.
40+
#[cfg(feature = "telegram-webapp-sdk")]
41+
#[cfg_attr(docsrs, doc(cfg(feature = "telegram-webapp-sdk")))]
42+
impl From<ValidationError> for AppError {
43+
fn from(err: ValidationError) -> Self {
44+
AppError::telegram_auth(err.to_string())
45+
}
46+
}
47+
48+
#[cfg(all(test, feature = "telegram-webapp-sdk"))]
49+
mod tests {
50+
use telegram_webapp_sdk::utils::validate_init_data::ValidationError;
51+
52+
use super::*;
53+
use crate::AppErrorKind;
54+
55+
#[test]
56+
fn validation_error_maps_to_telegram_auth() {
57+
let err: AppError = ValidationError::SignatureMismatch.into();
58+
assert!(matches!(err.kind, AppErrorKind::TelegramAuth));
59+
}
60+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
//! - `config` — `From<config::ConfigError>` mapping
4141
//! - `tokio` — `From<tokio::time::error::Elapsed>` mapping
4242
//! - `reqwest` — `From<reqwest::Error>` mapping
43+
//! - `telegram-webapp-sdk` —
44+
//! `From<telegram_webapp_sdk::utils::validate_init_data::ValidationError>`
45+
//! mapping
4346
//! - `serde_json` — support for structured JSON details in [`ErrorResponse`];
4447
//! also pulled transitively by `axum`
4548
//! - `multipart` — compatibility flag for Axum multipart

0 commit comments

Comments
 (0)