google-maps-services-go icon indicating copy to clipboard operation
google-maps-services-go copied to clipboard

Roads: Nearby Roads does not give error when trying to issue request without billing setup

Open twa16 opened this issue 5 years ago • 2 comments

Call to nearby roads API does not give same error about not having billing setup as the geocoding API does.

Steps to reproduce

  1. Create a basic project requesting snap to roads
  2. Use API key associated with project that does not have billing enabled.
  3. Issue request

One will get an empty response but no error. The geocoding API does return an error stating that billing must be enabled.

Code example

c, err := maps.NewClient(maps.WithAPIKey(os.Getenv("GOOGLE_MAPS_API_KEY")))
	if err != nil {
		log.Fatalf("fatal error: %s", err)
	}

	r := maps.NearestRoadsRequest{Points: utils.GeoPointsToGmapLatLngs(geoPoints)}

	resp, err := c.NearestRoads(context.Background(), &r)
	if err != nil {
		log.Fatalf("fatal error: %s", err)
	}

	pretty.Println(resp.SnappedPoints)

Stack trace

None

twa16 avatar Oct 04 '20 14:10 twa16

hi

May I confirm with you about the step to setup?

  1. create a project ( the billing is enabled by default )
  2. create an API key in by click Create credentials > API key.
  3. disable the billing for this project.

the error message will be shown as follow?

{
  "error": {
    "code": 404,
    "message": "Requested entity was not found.",
    "status": "NOT_FOUND"
  }
}

my raw query ( please replace YOU_KEY field )

curl --request GET \
  --url 'https://roads.googleapis.com/v1/nearestRoads?points=60.170880%2C24.942795%7C60.170879%2C24.942796%7C60.170877%2C24.942796&key=YOU_KEY'

my guess

I think it did throw an error, you can find err = json.NewDecoder(httpResp.Body).Decode(resp) will return an error as the response with error cannot mapped to resp which is a reference from response := &NearestRoadsResponse{}

func (c *Client) getJSON(ctx context.Context, config *apiConfig, apiReq apiRequest, resp interface{}) error {
	requestMetrics := c.metricReporter.NewRequest(config.path)
	httpResp, err := c.get(ctx, config, apiReq)
	if err != nil {
		requestMetrics.EndRequest(ctx, err, httpResp, "")
		return err
	}
	defer httpResp.Body.Close()

	err = json.NewDecoder(httpResp.Body).Decode(resp)
	requestMetrics.EndRequest(ctx, err, httpResp, httpResp.Header.Get("x-goog-maps-metro-area"))
	return err
}

AlphaWong avatar Oct 09 '20 04:10 AlphaWong

Thanks for the response. I agree that the underlying raw API call throws the error. I think the issue is that unlike the geocoding go call, the error does not get propagated up to the developer. There may very well be an internal error but to the developer using the gmaps golang library, it looks like a silent failure.

As for reproduction, billing was not enabled by default but I imagine having a project with billing explicitly disabled will reproduce the issue.

twa16 avatar Oct 10 '20 16:10 twa16