handleErrorResponse static method

UploadException handleErrorResponse(
  1. Response response
)

处理错误响应

Implementation

static UploadException handleErrorResponse(http.Response response) {
  final statusCode = response.statusCode;
  final body = response.body;

  print('❌ 上传失败: $statusCode');
  print('📄 响应内容: ${body.length > 200 ? body.substring(0, 200) : body}');

  switch (statusCode) {
    case 400:
      return UploadException(
        _i18n.get('error_400'),
        code: 'BAD_REQUEST',
        statusCode: statusCode,
      );
    case 401:
      return UploadException(
        _i18n.get('error_401'),
        code: 'UNAUTHORIZED',
        statusCode: statusCode,
      );
    case 403:
      return UploadException(
        _i18n.get('error_403'),
        code: 'FORBIDDEN',
        statusCode: statusCode,
      );
    case 404:
      return UploadException(
        _i18n.get('error_404'),
        code: 'NOT_FOUND',
        statusCode: statusCode,
      );
    case 413:
      return UploadException(
        _i18n.get('error_413'),
        code: 'FILE_TOO_LARGE',
        statusCode: statusCode,
      );
    case 500:
      return UploadException(
        _i18n.get('error_500'),
        code: 'INTERNAL_SERVER_ERROR',
        statusCode: statusCode,
      );
    case 502:
      return UploadException(
        _i18n.get('error_502'),
        code: 'BAD_GATEWAY',
        statusCode: statusCode,
      );
    case 503:
      return UploadException(
        _i18n.get('error_503'),
        code: 'SERVICE_UNAVAILABLE',
        statusCode: statusCode,
      );
    case 504:
      return UploadException(
        _i18n.get('error_504'),
        code: 'GATEWAY_TIMEOUT',
        statusCode: statusCode,
      );
    default:
      return UploadException(
        _i18n.format('error_default', {'code': statusCode}),
        code: 'HTTP_ERROR',
        statusCode: statusCode,
        details: body,
      );
  }
}