forked from googleapis/google-api-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
48 lines (42 loc) · 1.47 KB
/
error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2019 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gensupport
import (
"errors"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/googleapis/gax-go/v2/apierror"
"google.golang.org/api/googleapi"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/protobuf/proto"
)
func TestWrapError(t *testing.T) {
// The error format v2 for Google JSON REST APIs, per https://cloud.google.com/apis/design/errors#http_mapping.
jsonErrStr := "{\"error\":{\"details\":[{\"@type\":\"type.googleapis.com/google.rpc.ErrorInfo\", \"reason\":\"just because\", \"domain\":\"tests\"}]}}"
hae := &googleapi.Error{
Body: jsonErrStr,
}
hae = WrapError(hae)
var aerr *apierror.APIError
if ok := errors.As(hae, &aerr); !ok {
t.Errorf("got false, want true")
}
httpErrInfo := &errdetails.ErrorInfo{Reason: "just because", Domain: "tests"}
details := apierror.ErrDetails{ErrorInfo: httpErrInfo}
if diff := cmp.Diff(aerr.Details(), details, cmp.Comparer(proto.Equal)); diff != "" {
t.Errorf("got(-), want(+),: \n%s", diff)
}
if s := aerr.Reason(); s != "just because" {
t.Errorf("Reason() got %s, want 'just because'", s)
}
if s := aerr.Domain(); s != "tests" {
t.Errorf("Domain() got %s, want nil", s)
}
if err := aerr.Unwrap(); err != nil {
t.Errorf("Unwrap() got %T, want nil", err)
}
if m := aerr.Metadata(); m != nil {
t.Errorf("Metadata() got %v, want nil", m)
}
}