SmartError class sealed

SmartDio 统一错误基类(sealed class)

使用 Dart 3 sealed class 实现分层、类型安全的错误处理。 支持模式匹配(pattern matching)进行 exhaustive 处理。

子类:

示例:

try {
  await SmartDio.get('/api/data');
} catch (e) {
  final error = SmartError.from(e);
  final msg = switch (error) {
    NetworkError() => '网络连接失败',
    TimeoutError(:final phase) => switch (phase) {
      TimeoutPhase.connect => '连接超时',
      TimeoutPhase.send => '发送超时',
      TimeoutPhase.receive => '接收超时',
    },
    ResponseError(:final statusCode) => '服务器错误 ($statusCode)',
    ParseError() => '数据解析失败',
    BusinessError(:final serverMessage) => serverMessage,
    CancelError() => '请求已取消',
    CertificateError() => '证书验证失败',
    UnknownError() => '未知错误',
  };
}
Implemented types
Implementers

Constructors

SmartError.from(Object error, [StackTrace? stack])
从任意异常创建 SmartError
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
message String
用户友好的错误消息
final
originalError Object?
原始异常
final
requestOptions RequestOptions?
原始请求配置(可选,非 Dio 错误可能为空)
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
stackTrace StackTrace?
堆栈跟踪
final
timestamp DateTime
错误发生时间戳
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited