פתרון בעיות במקרה של שגיאות

בקטע הזה מוסבר איך לטפל בשגיאות.

טיפול בשגיאות של update_mask

כש-GMTDDeliveryVehicleReporter שולח עדכון לגבי רכב, יכולה להתרחש שגיאה אם פרמטר השאילתה 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