Skip to content

Commit

Permalink
Improves API error output for better debugging.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 628425474
  • Loading branch information
rvilgalys authored and copybara-github committed Apr 29, 2024
1 parent 81fe633 commit 2fb809d
Show file tree
Hide file tree
Showing 6 changed files with 812 additions and 7 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,15 @@ For testing the blocklists, you can use the following URLs:

## 4XX Errors

If you start the client without proper credentials or project set up, you will
see an error similar to what is shown below on startup:
If you start the client without proper credentials or project set up, you will see an error similar to what is shown below on startup:

```
webrisk: 2023/01/27 19:36:13 database.go:217: ListUpdate failure (1): webrisk: unexpected server response code: 400
```

For 400 errors, this usually means the API key is incorrect or was not supplied
correctly.
For 400 errors, this usually means the API key is incorrect or was not supplied correctly.

For 403 errors, this could mean the Web Risk API is not enabled for your project
**or** your project does not have Billing enabled.
For 403 errors, this could mean the Web Risk API is not enabled for your project **or** your project does not have Billing enabled.

# About the Social Engineering Extended Coverage List

Expand Down
18 changes: 17 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
err_pb "github.com/google/webrisk/internal/http_error_proto"
pb "github.com/google/webrisk/internal/webrisk_proto"
)

Expand Down Expand Up @@ -97,7 +98,7 @@ func (a *netAPI) doRequest(ctx context.Context, urlString string, resp proto.Mes
}
defer httpResp.Body.Close()
if httpResp.StatusCode != 200 {
return fmt.Errorf("webrisk: unexpected server response code: %d", httpResp.StatusCode)
return a.parseError(httpResp)
}
body, err := ioutil.ReadAll(httpResp.Body)
if err != nil {
Expand All @@ -106,6 +107,21 @@ func (a *netAPI) doRequest(ctx context.Context, urlString string, resp proto.Mes
return protojson.Unmarshal(body, resp)
}

// parseError parses an error JSON body and returns an error summary.
func (a *netAPI) parseError(httpResp *http.Response) error {
body, err := ioutil.ReadAll(httpResp.Body)
if err != nil {
return err
}
ep := new(err_pb.Error)
o := protojson.UnmarshalOptions{DiscardUnknown: true, AllowPartial: true}
if err := o.Unmarshal(body, ep); err != nil {
return fmt.Errorf("webrisk: unknown error, response code: %d", httpResp.StatusCode)
}
return fmt.Errorf("webrisk: unexpected server response code: %d, status: %s, message: %s",
httpResp.StatusCode, ep.GetError().GetStatus(), ep.GetError().GetMessage())
}

// ListUpdate issues a ComputeThreatListDiff API call and returns the response.
func (a *netAPI) ListUpdate(ctx context.Context, threatType pb.ThreatType, versionToken []byte,
compressionTypes []pb.CompressionType) (*pb.ComputeThreatListDiffResponse, error) {
Expand Down
70 changes: 70 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@
package webrisk

import (
"bytes"
"context"
"encoding/base64"
"errors"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
pb "github.com/google/webrisk/internal/webrisk_proto"
Expand Down Expand Up @@ -182,3 +188,67 @@ func TestNetAPI(t *testing.T) {
t.Errorf("unexpected HashLookup success, wanted malformed JSON error")
}
}

func createBody(j string) io.ReadCloser {
return ioutil.NopCloser(bytes.NewReader([]byte(j)))
}

func TestParseError(t *testing.T) {
tests := []struct {
httpResp *http.Response
wantErr error
}{
{
httpResp: &http.Response{
StatusCode: http.StatusBadRequest,
Body: createBody(`{
"error": {
"code": 400,
"message": "API key not valid. Please pass a valid API key",
"status": "INVALID_ARGUMENT"}}`), // Formatted Response
},
wantErr: errors.New("webrisk: unexpected server response code: 400, status: INVALID_ARGUMENT, message: API key not valid. Please pass a valid API key"),
},
{
httpResp: &http.Response{
StatusCode: http.StatusForbidden,
Body: createBody(`{
"error": {
"code": 403,
"message": "API Not Enabled",
"status": "PERMISSION_DENIED",
"details": [{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "PERMISSION_DENIED",
"domain": "googleapis.com",
"metadata": {
"service": "webrisk.googleapis.com"
}}]}}`), // Full Response
},
wantErr: errors.New("webrisk: unexpected server response code: 403, status: PERMISSION_DENIED, message: API Not Enabled"),
},
{
httpResp: &http.Response{
StatusCode: http.StatusServiceUnavailable,
Body: createBody(""), // Empty body
},
wantErr: errors.New("webrisk: unknown error, response code: 503"),
},
{
httpResp: &http.Response{
StatusCode: http.StatusInternalServerError,
Body: createBody(`{badjson}`),
},
wantErr: errors.New("webrisk: unknown error, response code: 500"),
},
}

for _, tc := range tests {
var a netAPI
gotErr := a.parseError(tc.httpResp)

if cmp.Equal(tc.wantErr, gotErr, cmpopts.EquateErrors()) {
t.Errorf("parseError(%v) returned error: %v, want error: %v", tc.httpResp, gotErr, tc.wantErr)
}
}
}
Loading

0 comments on commit 2fb809d

Please sign in to comment.