...

Source file src/google.golang.org/api/internal/gensupport/error_test.go

Documentation: google.golang.org/api/internal/gensupport

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

View as plain text