error.go raw

   1  // Copyright 2022 Google LLC. All rights reserved.
   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  
  10  	"github.com/googleapis/gax-go/v2/apierror"
  11  	"google.golang.org/api/googleapi"
  12  )
  13  
  14  // WrapError creates an [apierror.APIError] from err, wraps it in err, and
  15  // returns err. If err is not a [googleapi.Error] (or a
  16  // [google.golang.org/grpc/status.Status]), it returns err without modification.
  17  func WrapError(err error) error {
  18  	var herr *googleapi.Error
  19  	apiError, ok := apierror.ParseError(err, false)
  20  	if ok && errors.As(err, &herr) {
  21  		herr.Wrap(apiError)
  22  	}
  23  	return err
  24  }
  25