Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bq.py can't handle notFound error #54

Closed
otofune opened this issue Oct 5, 2022 · 2 comments · Fixed by #55
Closed

bq.py can't handle notFound error #54

otofune opened this issue Oct 5, 2022 · 2 comments · Fixed by #55

Comments

@otofune
Copy link

otofune commented Oct 5, 2022

For example, bq --api="http://localhost:9050" --project_id="test" mk --dataset test (with ./bigquery-emulator --project test) failed with following error:

BigQuery error in mk operation: Could not connect with BigQuery server.
Http response status: 404
Http response content:
b'dataset test is not found'

bq checks if dataset/table/etc already exists before creating it. Of course bigquery-emulator already returns 404, but bq can't handle plain text error.

fmt.Fprintf(w, "job %s is not found", jobID)

I fixed errors by following https://cloud.google.com/bigquery/docs/error-messages with minimal example:

--- a/server/middleware.go
+++ b/server/middleware.go
diff --git a/server/middleware.go b/server/middleware.go
index 7a51ae9..25a8d8a 100644
--- a/server/middleware.go
+++ b/server/middleware.go
@@ -153,8 +153,18 @@ func withDatasetMiddleware() func(http.Handler) http.Handler {
                                dataset := project.Dataset(datasetID)
                                if dataset == nil {
                                        logger.Logger(ctx).Info("dataset is not found", zap.String("datasetID", datasetID))
+                                       w.Header().Add("Content-Type", "application/json")
                                        w.WriteHeader(http.StatusNotFound)
-                                       fmt.Fprintf(w, "dataset %s is not found", datasetID)
+                                       encodeResponse(ctx, w, map[string]interface{}{
+                                               "error": map[string]interface{}{
+                                                       "errors": []interface{}{
+                                                               map[string]interface{}{
+                                                                       "message": fmt.Sprintf("dataset %s is not found", datasetID),
+                                                                       "reason":  "notFound",
+                                                               },
+                                                       },
+                                               },
+                                       })
                                        return
                                }
                                ctx = withDataset(ctx, dataset)
@goccy
Copy link
Owner

goccy commented Oct 6, 2022

I read the error dofcumentation ( https://cloud.google.com/bigquery/docs/reference/rest/v2/ErrorProto ).
Also, when I read the source code of bq.py, it seems that notFound must be returned as the reason. I'll fix it !

@goccy
Copy link
Owner

goccy commented Oct 7, 2022

I fixed this problem with v0.1.15 . Thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants