shenzhencenter / google-ads-pb

Google Ads API Client Library for Golang
Apache License 2.0
45 stars 15 forks source link

verbose error messages #24

Closed jimmyl02 closed 5 months ago

jimmyl02 commented 5 months ago

Hi, thanks for maintaining this project. I was wondering if there is a way to get more verbose error messages when something goes wrong?

With this golang library I see error messages like the following but it is hard to debug what is wrong

rpc error: code = InvalidArgument desc = Request contains an invalid argument.

Using the official python sdk the error messages are much more verbose like the following example.

  error_code {
    collection_size_error: TOO_FEW
  }
  message: "Too few."
  location {
    field_path_elements {
      field_name: "operations"
      index: 0
    }
    field_path_elements {
      field_name: "create"
    }
    field_path_elements {
      field_name: "ad"
    }
    field_path_elements {
      field_name: "responsive_search_ad"
    }
    field_path_elements {
      field_name: "descriptions"
    }
  }
}

Is it supported / possible to get these verbose error messages with the current library? Appreciate any help / pointers, thanks!

jimmyl02 commented 5 months ago

actually to save some time for others who may find this thread, looks like Google helpfully returns a detailed error in the grpc response, we just need to convert the error to a status and you can then get the details via

respStatus := status.Convert(err)
fmt.Println(status.Details())
olderyi commented 5 months ago

It's great, I'm also looking for a detailed error in GRPC, but I only found one GoogleAdsError and I don't know how to use it In addition, this code is a little error:

respStatus := status.Convert(err)
fmt.Println(respStatus .Details())
olderyi commented 5 months ago

I think I know how to use GoogleAdsError. This is an example

link, err := clinkServiceClient.MutateCustomerClientLink(ctx, linkCustomer)
if err != nil {
  apiErr := status.Convert(err)
  for _, detail := range apiErr.Details() {
      adsError := detail.(*errors.GoogleAdsFailure)
      errs := adsError.GetErrors()
      for _, googleErr := range errs {
          fmt.Printf("\n\nError message: \t\t\t\t %s\n", googleErr.GetMessage())
          fmt.Printf("Error code: \t\t\t\t %s\n", googleErr.GetErrorCode())
          errCode := googleErr.GetErrorCode()
          x2 := errCode.GetCustomerClientLinkError()
          fmt.Printf("Error customer_client_link_error: \t %+v\n", x2)
          fmt.Printf("Error trigger: \t\t\t\t %s\n", googleErr.GetTrigger())
          location := googleErr.GetLocation()
          for _, loc := range location.GetFieldPathElements() {
              fmt.Printf("Error field_path_elements: index:%+v name: %+v\n", loc.GetIndex(), loc.GetFieldName())
          }
      }
      fmt.Printf("Error request_id: \t\t\t %+v\n", adsError.GetRequestId())
  }
  return "", err
}