github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/internal/getproviders/errors.go (about)

     1  package getproviders
     2  
     3  import (
     4  	"fmt"
     5  
     6  	svchost "github.com/hashicorp/terraform-svchost"
     7  	"github.com/hashicorp/terraform/addrs"
     8  )
     9  
    10  // ErrHostNoProviders is an error type used to indicate that a hostname given
    11  // in a provider address does not support the provider registry protocol.
    12  type ErrHostNoProviders struct {
    13  	Hostname svchost.Hostname
    14  
    15  	// HasOtherVersionis set to true if the discovery process detected
    16  	// declarations of services named "providers" whose version numbers did not
    17  	// match any version supported by the current version of Terraform.
    18  	//
    19  	// If this is set, it's helpful to hint to the user in an error message
    20  	// that the provider host may be expecting an older or a newer version
    21  	// of Terraform, rather than that it isn't a provider registry host at all.
    22  	HasOtherVersion bool
    23  }
    24  
    25  func (err ErrHostNoProviders) Error() string {
    26  	switch {
    27  	case err.HasOtherVersion:
    28  		return fmt.Sprintf("host %s does not support the provider registry protocol required by this Terraform version, but may be compatible with a different Terraform version", err.Hostname.ForDisplay())
    29  	default:
    30  		return fmt.Sprintf("host %s does not offer a Terraform provider registry", err.Hostname.ForDisplay())
    31  	}
    32  }
    33  
    34  // ErrHostUnreachable is an error type used to indicate that a hostname
    35  // given in a provider address did not resolve in DNS, did not respond to an
    36  // HTTPS request for service discovery, or otherwise failed to correctly speak
    37  // the service discovery protocol.
    38  type ErrHostUnreachable struct {
    39  	Hostname svchost.Hostname
    40  	Wrapped  error
    41  }
    42  
    43  func (err ErrHostUnreachable) Error() string {
    44  	return fmt.Sprintf("could not connect to %s: %s", err.Hostname.ForDisplay(), err.Wrapped.Error())
    45  }
    46  
    47  // Unwrap returns the underlying error that occurred when trying to reach the
    48  // indicated host.
    49  func (err ErrHostUnreachable) Unwrap() error {
    50  	return err.Wrapped
    51  }
    52  
    53  // ErrUnauthorized is an error type used to indicate that a hostname
    54  // given in a provider address returned a "401 Unauthorized" or "403 Forbidden"
    55  // error response when we tried to access it.
    56  type ErrUnauthorized struct {
    57  	Hostname svchost.Hostname
    58  
    59  	// HaveCredentials is true when the request that failed included some
    60  	// credentials, and thus it seems that those credentials were invalid.
    61  	// Conversely, HaveCredentials is false if the request did not include
    62  	// credentials at all, in which case it seems that credentials must be
    63  	// provided.
    64  	HaveCredentials bool
    65  }
    66  
    67  func (err ErrUnauthorized) Error() string {
    68  	switch {
    69  	case err.HaveCredentials:
    70  		return fmt.Sprintf("host %s rejected the given authentication credentials", err.Hostname)
    71  	default:
    72  		return fmt.Sprintf("host %s requires authentication credentials", err.Hostname)
    73  	}
    74  }
    75  
    76  // ErrProviderNotKnown is an error type used to indicate that the hostname
    77  // given in a provider address does appear to be a provider registry but that
    78  // registry does not know about the given provider namespace or type.
    79  //
    80  // A caller serving requests from an end-user should recognize this error type
    81  // and use it to produce user-friendly hints for common errors such as failing
    82  // to specify an explicit source for a provider not in the default namespace
    83  // (one not under registry.terraform.io/hashicorp/). The default error message
    84  // for this type is a direct description of the problem with no such hints,
    85  // because we expect that the caller will have better context to decide what
    86  // hints are appropriate, e.g. by looking at the configuration given by the
    87  // user.
    88  type ErrProviderNotKnown struct {
    89  	Provider addrs.Provider
    90  }
    91  
    92  func (err ErrProviderNotKnown) Error() string {
    93  	return fmt.Sprintf(
    94  		"provider registry %s does not have a provider named %s",
    95  		err.Provider.Hostname.ForDisplay(),
    96  		err.Provider,
    97  	)
    98  }
    99  
   100  // ErrPlatformNotSupported is an error type used to indicate that a particular
   101  // version of a provider isn't available for a particular target platform.
   102  //
   103  // This is returned when DownloadLocation encounters a 404 Not Found response
   104  // from the underlying registry, because it presumes that a caller will only
   105  // ask for the DownloadLocation for a version it already found the existence
   106  // of via AvailableVersions.
   107  type ErrPlatformNotSupported struct {
   108  	Provider addrs.Provider
   109  	Version  Version
   110  	Platform Platform
   111  }
   112  
   113  func (err ErrPlatformNotSupported) Error() string {
   114  	return fmt.Sprintf(
   115  		"provider %s %s is not available for %s",
   116  		err.Provider,
   117  		err.Version,
   118  		err.Platform,
   119  	)
   120  }
   121  
   122  // ErrQueryFailed is an error type used to indicate that the hostname given
   123  // in a provider address does appear to be a provider registry but that when
   124  // we queried it for metadata for the given provider the server returned an
   125  // unexpected error.
   126  //
   127  // This is used for any error responses other than "Not Found", which would
   128  // indicate the absense of a provider and is thus reported using
   129  // ErrProviderNotKnown instead.
   130  type ErrQueryFailed struct {
   131  	Provider addrs.Provider
   132  	Wrapped  error
   133  }
   134  
   135  func (err ErrQueryFailed) Error() string {
   136  	return fmt.Sprintf(
   137  		"could not query provider registry for %s: %s",
   138  		err.Provider.String(),
   139  		err.Wrapped.Error(),
   140  	)
   141  }
   142  
   143  // Unwrap returns the underlying error that occurred when trying to reach the
   144  // indicated host.
   145  func (err ErrQueryFailed) Unwrap() error {
   146  	return err.Wrapped
   147  }