排解錯誤

本節說明如何處理錯誤。

處理 update_mask 錯誤

updateMask 查詢參數空白時,可能會發生 update_mask 錯誤。為避免發生這項錯誤,請務必至少提供一個欄位名稱。這通常會發生在啟動後的第一次更新。如要進一步瞭解如何在 Fleet Engine 中更新車輛欄位,請參閱「更新車輛欄位」一文。

以下範例說明如何處理這項錯誤:

Swift

import GoogleRidesharingDriver

class VehicleReporterListener: NSObject, GMTDVehicleReporterListener {
  func vehicleReporter(
    _ vehicleReporter: GMTDVehicleReporter,
    didFail vehicleUpdate: GMTDVehicleUpdate,
    withError error: Error
  ) {
    let fullError = error as NSError
    if let innerError = fullError.userInfo[NSUnderlyingErrorKey] as? NSError {
      let innerFullError = innerError as NSError
      if innerFullError.localizedDescription.contains("update_mask cannot be empty") {
        emptyMaskUpdates += 1
        return
      }
    }
    failedUpdates += 1
  }

  override init() {
    emptyMaskUpdates = 0
    failedUpdates = 0
  }
}

Objective-C

#import "VehicleReporterListener.h"
#import <GoogleRidesharingDriver/GoogleRidesharingDriver.h>

@implementation VehicleReporterListener {
  NSInteger emptyMaskUpdates = 0;
  NSInteger failedUpdates = 0;
}

-   (void)vehicleReporter:(GMTDVehicleReporter *)vehicleReporter
   didFailVehicleUpdate:(GMTDVehicleUpdate *)vehicleUpdate
              withError:(NSError *)error {
  for (NSError *underlyingError in error.underlyingErrors) {
    if ([underlyingError.localizedDescription containsString:@"update_mask cannot be empty"]) {
      emptyMaskUpdates += 1;
      return;
    }
  }
  failedUpdates += 1
}

@end