pub struct ApiResponse<T> {
pub code: i32,
pub data: Option<T>,
pub reason: Option<String>,
pub message: Option<String>,
pub track: Option<String>,
}Expand description
统一 API 响应
使用 code 标记是否成功:
- code = 200: 成功
- code > 0: 错误
Fields§
§code: i32状态码 (0=成功, >0=错误)
data: Option<T>响应数据
reason: Option<String>错误原因 (仅错误时有值)
message: Option<String>错误消息 (仅错误时有值)
track: Option<String>追踪 ID
Implementations§
Source§impl<T> ApiResponse<T>
impl<T> ApiResponse<T>
Sourcepub fn success(data: T) -> ApiResponse<T>
pub fn success(data: T) -> ApiResponse<T>
创建成功响应
Sourcepub fn error(
code: i32,
reason: impl Into<String>,
message: impl Into<String>,
) -> ApiResponse<T>
pub fn error( code: i32, reason: impl Into<String>, message: impl Into<String>, ) -> ApiResponse<T>
创建错误响应
Sourcepub fn error_with_track(
code: i32,
reason: impl Into<String>,
message: impl Into<String>,
track: impl Into<String>,
) -> ApiResponse<T>
pub fn error_with_track( code: i32, reason: impl Into<String>, message: impl Into<String>, track: impl Into<String>, ) -> ApiResponse<T>
创建带追踪 ID 的错误响应
Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
判断是否成功
Sourcepub fn from_code(code: ErrorCode) -> ApiResponse<T>
pub fn from_code(code: ErrorCode) -> ApiResponse<T>
从 ErrorCode 创建错误响应
Source§impl<T> ApiResponse<T>
impl<T> ApiResponse<T>
Sourcepub fn from_proto_error(detail: &ErrorDetail) -> ApiResponse<T>
pub fn from_proto_error(detail: &ErrorDetail) -> ApiResponse<T>
从 ProtoErrorDetail 转换
Sourcepub fn to_proto_error(&self) -> Option<ErrorDetail>
pub fn to_proto_error(&self) -> Option<ErrorDetail>
转换为 ProtoErrorDetail
Trait Implementations§
Source§impl<T> Debug for ApiResponse<T>where
T: Debug,
impl<T> Debug for ApiResponse<T>where
T: Debug,
Source§impl<'de, T> Deserialize<'de> for ApiResponse<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for ApiResponse<T>where
T: Deserialize<'de>,
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<ApiResponse<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<ApiResponse<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl<T> From<FlareError> for ApiResponse<T>
从 FlareError 转换为 ApiResponse
impl<T> From<FlareError> for ApiResponse<T>
从 FlareError 转换为 ApiResponse
转换规则:
- HTTP 错误码 (400-599): 保持原样
- 其他错误码: 统一转换为 500 (HttpInternalServerError)
Source§fn from(err: FlareError) -> ApiResponse<T>
fn from(err: FlareError) -> ApiResponse<T>
Converts to this type from the input type.
Source§impl<T> Serialize for ApiResponse<T>where
T: Serialize,
impl<T> Serialize for ApiResponse<T>where
T: Serialize,
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Serialize this value into the given Serde serializer. Read more
Auto Trait Implementations§
impl<T> Freeze for ApiResponse<T>
impl<T> RefUnwindSafe for ApiResponse<T>where
Option<T>: RefUnwindSafe,
impl<T> Send for ApiResponse<T>
impl<T> Sync for ApiResponse<T>
impl<T> Unpin for ApiResponse<T>
impl<T> UnsafeUnpin for ApiResponse<T>where
Option<T>: UnsafeUnpin,
impl<T> UnwindSafe for ApiResponse<T>where
Option<T>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request