Binary Protocol
Type Mapping
| Rust Type | TS/JS Type | Kotlin Type | C# Type |
|---|---|---|---|
i8 | number | Byte | sbyte |
u8 | number | Byte | byte |
i16 | number | Short | short |
u16 | number | Short | ushort |
i32 | number | Int | int |
u32 | number | Int | uint |
i64 | number | Long | long |
u64 | number | Long | ulong |
f32 | number | Float | float |
f64 | number | Double | double |
bool | boolean | Boolean | bool |
String, &str | string | String | string |
Vec<u8> | Uint8Array | ByteArray | byte[] |
Option<T> | T | null | T? | T? |
Vec<T> | T[] | List<T> | List<T> |
| struct | { field: Type } | data class | class |
| enum | { tag: 'Variant', data: ... } | sealed class | abstract record + sealed record |
Error Codes
System reserved error codes range from -90012 to -90000. User-defined errors must not use this range.
| Constant | Value | Description |
|---|---|---|
CODE_SIGNAL | -90000 | OS signal (e.g., Ctrl+C) |
CODE_MSG_TOO_SHORT | -90001 | Message too short |
CODE_PAYLOAD_MISMATCH | -90002 | Payload length mismatch |
CODE_SERIALIZE | -90003 | Serialization/deserialization error |
CODE_STATE_NOT_FOUND | -90004 | State type not registered |
CODE_HANDLER | -90005 | Handler execution error |
CODE_INVALID_PARAM | -90006 | Invalid parameter |
CODE_IO | -90007 | I/O error |
CODE_WS | -90008 | WebSocket error |
CODE_HTTP | -90009 | HTTP error |
CODE_TCP | -90010 | TCP error |
CODE_LONG_CONNECTION_NOT_SUPPORTED | -90011 | Long connections unsupported in HTTP mode |
CODE_RATE_LIMITED | -90012 | Rate limit exceeded |
#![allow(unused)]
fn main() {
// Custom error (code must be outside the reserved range)
return Err(afast::Error::custom(400, "invalid request parameter"));
}
Custom Error Types
Implement the AFastError trait to define your own error types and return Result<T, MyError> from handlers:
#![allow(unused)]
fn main() {
use afast::AFastError;
enum MyError {
NotFound(String),
Unauthorized,
}
impl AFastError for MyError {
fn code(&self) -> i64 {
match self {
MyError::NotFound(_) => 404,
MyError::Unauthorized => 401,
}
}
fn message(&self) -> String {
match self {
MyError::NotFound(name) => format!("{} not found", name),
MyError::Unauthorized => "unauthorized".into(),
}
}
}
#[handler(desc("Get user"))]
async fn get_user(id: Data<UserId>) -> afast::Result<UserInfo, MyError> {
find_user(id).ok_or(MyError::NotFound("user".into()))
}
}
All handler macros (#[handler], #[get]/#[post]/#[put]/#[delete], #[ws], #[sse]) support custom error types. afast::Result<T> defaults to Error and is fully backward compatible.