七牛 iOS SDK 是否处理 502 错误时代码不严谨?

同事在收到 502 错误时, sdk 跑蹦, 因此调源码:
参考源码:
https://github.com/qiniu/objc-sdk

源码修改:
QNResponseInfo.m 文件: 在 init 函数里 NSDictionary *uInfo 有可能是 nil 502错误没有json格式数据

- (instancetype)init:(int)status
withReqId:(NSString *)reqId
withXLog:(NSString *)xlog
withBody:(NSData *)body {

if (self = [super init]) {
    _statusCode = status;
    _reqId = [reqId copy];
    _xlog = [xlog copy];
    if (status != 200) {
        if (body == nil) {
            _error = [[NSError alloc] initWithDomain:domain code:_statusCode userInfo:nil];
        } else {
            NSError *tmp;
            NSDictionary *uInfo = [NSJSONSerialization JSONObjectWithData:body options:NSJSONReadingMutableLeaves error:&tmp];

            /* 这段需要做 uInfo nil 判断, 否则代码执行会蹦 */
            if (tmp != nil && uInfo != nil) {
                uInfo = @{ @"error":[[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding] };
            }
            _error = [[NSError alloc] initWithDomain:domain code:_statusCode userInfo:uInfo];
        }
    }
    else if (body == nil || body.length == 0) {
        NSDictionary *uInfo = @{ @"error":@"no response json" };
        _error = [[NSError alloc] initWithDomain:domain code:_statusCode userInfo:uInfo];
    }
}
return self;

}

阅读 4.2k
2 个回答

非常感谢你的建议,我们本地还原下场景,能否告知下info中间返回502有什么具体的报错信息,代码修改的问题你可以直接在 https://github.com/qiniu/objc-sdk/issues 中新建issue进行反馈。

您好,已经在github回复了,麻烦您再确认下,要不我们在qq上讨论下

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进