cloudkit_js saveRecord错误处理

时间:2016-05-06 21:36:44

标签: java error-handling cloudkit

在cloudkit_js中,saveRecord的错误处理对我不起作用。这是一个小例子:

            var location = {
                        latitude: self.newCameraLatitude(),
                        longitude: self.newCameraLongitude()
            };

            var record = {
                recordType: "The Thing",
                fields: {
                    title: { value: self.newThingTitle() },
                    location: { value: location },
                }
            };

            privateDB.saveRecord(record).then(
                function(response) {
                    if (response.hasErrors) {
                        console.log('Might need to call Huston');
                        console.error(response.errors[0]);
                    } else {
                     // do something
                    }
                });

如果输入参数正常,代码会将记录保存到专用数据库。 当Lat / lng具有超出范围的值时,在保存期间发生错误。该错误不会被if(response.hasErrors)所困。

这是错误:

Failed to load resource: the server responded with a status of 400 (Bad Request)

当我在Web Inspector控制台中单击它时,我得到了这个:

{
    "uuid": "a7d41349-b9f0-499c-9d30-43b7c23e6355",
    "serverErrorCode": "BAD_REQUEST",
    "reason": "invalid attempt to use latitude outside valid range of -90.0 to 90.0"
}

所以...我理解错误 - 这不是问题。 问题是我想捕获错误并与用户一起处理,但是如果(response.hasErrors)'似乎没有捕获它。

有什么想法吗?

  • 大卫

1 个答案:

答案 0 :(得分:0)

这就是我如何捕获错误并处理它......

            privateDB.saveRecords(record)
            .then(function(response) {
                    if (response.hasErrors) {
                        throw response.errors[0];
                    } else {
                     // do something
                    }
                })
                .catch(function(error) {
                      console.log('Gota catch em all -->'+error);
                      // handle the jandle! :-)
                      self.saveButtonEnabled(true);
                });

.catch收到"错误" - 这是一个CKError,看起来像这样:

[CKError
ckErrorCode: BAD_REQUEST
extensionErrorCode: undefined
isError: true
isServerError: true
isServerExtensionError: true
message: invalid attempt to use longitude outside valid range of -180.0 to 180.0
name: Error
reason: invalid attempt to use longitude outside valid range of -180.0 to 180.0
recordName: undefined
redirectURL: undefined
retryAfter: undefined
serverErrorCode: BAD_REQUEST
subscriptionID: undefined
uuid: 6feaa17b-8bc2-4b98-a1d2-3a0ac5bbb4da
zoneID: undefined
]
  • 大卫