googleapis / gapic-generator-python

Generate Python API client libraries from Protocol Buffers.
Apache License 2.0
122 stars 69 forks source link

Improve gRPC error message when request is invalid #2106

Open ithomaslin opened 5 months ago

ithomaslin commented 5 months ago

Hi folks,

from google.cloud.run_v2 import ListJobsRequest
from google.oauth2 import service_account
import google.cloud.run_v2 as run_v2

credentials = service_account.Credentials.from_service_account_file("key.json")

run_client = run_v2.ServicesClient(credentials=credentials)

request = ListJobsRequest(
    parent="parent_name"
)

run_client.list_jobs(request=request)

With the above code, which is following the sample provided in the official documentation. However, I got this error message:

grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
    status = StatusCode.UNIMPLEMENTED
    details = "Operation is not implemented, or supported, or enabled."
    debug_error_string = "UNKNOWN:Error received from peer ipv6:%5B2404:6800:4012::200a%5D:443 {created_time:"2024-05-31T19:40:11.155916+08:00", grpc_status:12, grpc_message:"Operation is not implemented, or supported, or enabled."}"
>
parthea commented 5 months ago

Hi @ithomaslin ,

This appears to be an issue with the API. Thanks for reporting the issue in the API specific issue tracker (https://issuetracker.google.com/issues/343845155). We'll keep this issue open and update it as more information is available.

ajoy39 commented 5 months ago

I am also getting this error when trying to use runjob, I followed a link from an issue that was last updated in Novemeber with the same problem so I am not sure if this is truly an issue with the API or rather it's our code but the python client (which is still version 0.X to be fair) is giving the wrong error return.

Here's the link to the issue I mentioned, last two comments relate to Cloud Run Jobs https://github.com/googleapis/python-datacatalog/issues/33

I also added a comment to the issue @parthea linked as well

parthea commented 5 months ago

I was able to re-create the error using the following code

from google.cloud.run_v2 import ListJobsRequest
import google.cloud.run_v2 as run_v2

run_client = run_v2.JobsClient()

request = ListJobsRequest(
    parent="projects/<project ID>"
)

run_client.list_jobs(request=request)

I switched from gRPC to REST transport by setting the transport argument of JobsClient to "rest", and found a more helpful error message that suggested the value of the parent argument is incorrect.

from google.cloud.run_v2 import ListJobsRequest
import google.cloud.run_v2 as run_v2

run_client = run_v2.JobsClient(transport="rest")

request = ListJobsRequest(
    parent="projects/<project ID>"
)

run_client.list_jobs(request=request)

Error when using REST transport.

ValueError: Invalid request.
Some of the fields of the request message are either not initialized or initialized with an invalid value.
Please make sure your request matches at least one accepted HTTP binding.
To match a binding the request message must have all the required fields initialized with values matching their patterns as listed below:
        URI: "/v2/{parent=projects/*/locations/*}/jobs"
        Required request fields:
                field: "parent", pattern: "projects/*/locations/*"

Essentially, the value for the parent argument of ListJobsRequest must be in the form projects/<your project>/locations/<your location> which matches what is documented.

See the updated code below which worked

from google.cloud.run_v2 import ListJobsRequest
import google.cloud.run_v2 as run_v2

run_client = run_v2.JobsClient()

request = ListJobsRequest(
    parent="projects/<your project>/locations/<your location>"
)

run_client.list_jobs(request=request)

https://github.com/googleapis/google-cloud-python/blob/d2a282512c457c5b348aeef118b6ea7df5a2bb6f/packages/google-cloud-run/google/cloud/run_v2/types/job.py#L142-L146

We'll keep this issue open as the error message from gRPC should be improved to the level that we see when using REST transport.

ajoy39 commented 5 months ago

I will also switch to the REST transport and see if I am running into something similar with run job, fwiw this is my code

def start_job_if_non_started(sender, **kwargs):
    client = run_v2.JobsClient()
    request: RunJobRequest = run_v2.RunJobRequest(
        name=f"projects/{app_settings.PROJECT_ID}/location/{app_settings.REGION}/jobs/{app_settings.SINGLE_JOB_QUEUE}"
    )

    try:
        client.run_job(request=request)
    except Exception as e:
        print(e)

Secondarily, part of the issue might be docs related, this is the doc for RunJobRequest, the text description of validate makes me think these docs might not be accurate, because I'm not sure how RunJob would delete resources in GCP

parthea commented 5 months ago

Thanks for the update @ajoy39. Please let me know if you're still unable to resolve the issue after trying REST. I've reached out to the API team to request a fix for the docs issue (Googlers see cl/639895039).

ajoy39 commented 5 months ago

sigh sorry for wasting your time y'all, my problem ended up being a simple typo. location instead of locations in the job name. I did miss that because of the vague error message (seems more like Exception not implemented than Endpoint not implemented 😀 ) but thats on me lol

tldr: Docs are fine, gRPC Transport ate the error message like it did for OP, but underlying issue was a typo

parthea commented 5 months ago

@ajoy39 Thanks for the update. I'm glad that you found the issue! I'll keep this issue open as the error message for gRPC could be improved.

parthea commented 2 months ago

I'm going to transfer this issue to the repository for the code generator where we can work to improve the gRPC error message.