/// API 异常类
class ApiException implements Exception {
  final String message;
  final String code;
  final int? statusCode;

  ApiException({
    required this.message,
    required this.code,
    this.statusCode,
  });

  factory ApiException.unauthorized([String? message]) {
    return ApiException(
      message: message ?? '未授权',
      code: '0002',
      statusCode: 401,
    );
  }

  factory ApiException.networkError([String? message]) {
    return ApiException(
      message: message ?? '网络错误',
      code: 'NETWORK_ERROR',
    );
  }

  factory ApiException.serverError([String? message]) {
    return ApiException(
      message: message ?? '服务器错误',
      code: 'SERVER_ERROR',
      statusCode: 500,
    );
  }

  @override
  String toString() => 'ApiException: $message (code: $code)';
}
