LarkAPIError

Enum LarkAPIError 

Source
pub enum LarkAPIError {
    IOErr(String),
    IllegalParamError(String),
    DeserializeError(String),
    RequestError(String),
    UrlParseError(String),
    ApiError {
        code: i32,
        message: String,
        request_id: Option<String>,
    },
    MissingAccessToken,
    BadRequest(String),
    DataError(String),
    APIError {
        code: i32,
        msg: String,
        error: Option<String>,
    },
}
Expand description

飞书开放平台API错误类型

包含所有可能的API调用错误,提供详细的错误信息和处理建议。 支持错误分类、重试判断和用户友好的错误消息。

§错误类型分类

  • 网络错误: RequestError, IOErr, UrlParseError
  • 数据错误: DeserializeError, DataError
  • 参数错误: IllegalParamError, BadRequest
  • API错误: ApiError, APIError
  • 认证错误: MissingAccessToken

§错误处理示例

use open_lark::core::error::LarkAPIError;

fn handle_api_error(error: LarkAPIError) {
    match error {
        LarkAPIError::MissingAccessToken => {
            println!("请检查应用凭据配置");
        }
        LarkAPIError::ApiError { code, message, .. } if code == 403 => {
            println!("权限不足: {}", message);
        }
        err if err.is_retryable() => {
            println!("网络错误,可以重试: {}", err.user_friendly_message());
        }
        _ => {
            println!("操作失败: {}", error.user_friendly_message());
        }
    }
}

§最佳实践

  • 使用 is_retryable() 判断是否可以重试
  • 使用 user_friendly_message() 获取用户友好的错误提示
  • 使用 is_permission_error() 检查权限相关错误

Variants§

§

IOErr(String)

输入输出错误

通常由文件操作、网络IO等底层操作失败引起。

§

IllegalParamError(String)

非法参数错误

当传入的参数不符合API要求时抛出,如无效的ID格式、超出范围的值等。

§

DeserializeError(String)

JSON反序列化错误

当API响应的JSON格式无法解析为预期的数据结构时发生。

§

RequestError(String)

HTTP请求失败

网络请求层面的错误,如连接超时、DNS解析失败等。通常可以重试。

§

UrlParseError(String)

URL解析错误

当构建的API请求URL格式不正确时发生。

§

ApiError

增强的API错误

包含错误码、消息和请求ID的完整错误信息,便于调试和问题追踪。

Fields

§code: i32

API错误码

§message: String

错误消息

§request_id: Option<String>

请求ID,用于问题追踪

§

MissingAccessToken

缺少访问令牌

当API调用需要认证但未提供有效的访问令牌时发生。

§

BadRequest(String)

错误的请求

请求格式或内容不符合API规范。

§

DataError(String)

数据处理错误

数据验证、转换或处理过程中发生的错误。

§

APIError

标准API响应错误

飞书开放平台返回的标准错误响应,包含完整的错误信息。

Fields

§code: i32

API错误码

§msg: String

错误消息

§error: Option<String>

详细错误信息

Implementations§

Source§

impl LarkAPIError

Source

pub fn api_error<M: Into<String>>( code: i32, message: M, request_id: Option<String>, ) -> Self

创建包含上下文信息的API错误

§参数
  • code: 错误码
  • message: 错误消息
  • request_id: 请求ID,用于问题追踪
§示例
use open_lark::core::error::LarkAPIError;

let error = LarkAPIError::api_error(
    403,
    "权限不足",
    Some("req_123456".to_string())
);
Source

pub fn illegal_param<T: Into<String>>(message: T) -> Self

创建非法参数错误

§参数
  • message: 错误详细信息
§示例
use open_lark::core::error::LarkAPIError;

let error = LarkAPIError::illegal_param("用户ID格式不正确");
Source

pub fn is_permission_error(&self) -> bool

检查是否为权限相关错误

用于判断错误是否由权限不足引起,便于进行相应的错误处理。

§返回值
  • true: 权限相关错误
  • false: 其他类型错误
Source

pub fn is_retryable(&self) -> bool

检查错误是否可以重试

判断当前错误是否为临时性错误,可以通过重试解决。 通常网络超时、连接失败等错误可以重试。

§返回值
  • true: 可以重试的错误
  • false: 不可重试的错误(如参数错误、权限错误)
§示例
use open_lark::core::error::LarkAPIError;

let error = LarkAPIError::RequestError("连接超时".to_string());
if error.is_retryable() {
    println!("可以重试该请求");
}
Source

pub fn user_friendly_message(&self) -> String

获取用户友好的错误消息

将技术性的错误信息转换为用户容易理解的提示信息。 包含错误原因和可能的解决建议。

§返回值

经过本地化和优化的错误消息字符串

§示例
use open_lark::core::error::LarkAPIError;

let error = LarkAPIError::MissingAccessToken;
println!("错误提示: {}", error.user_friendly_message());
// 输出: "缺少访问令牌,请检查认证配置"

Trait Implementations§

Source§

impl Clone for LarkAPIError

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LarkAPIError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for LarkAPIError

Source§

fn fmt(&self, __formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for LarkAPIError

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<Error> for LarkAPIError

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for LarkAPIError

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for LarkAPIError

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<ParseError> for LarkAPIError

Source§

fn from(err: ParseError) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,