github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/charmhub/errors.go (about)

     1  // Copyright 2021 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package charmhub
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/charmhub/transport"
    10  )
    11  
    12  // Handle some of the basic error messages.
    13  func handleBasicAPIErrors(list transport.APIErrors, logger Logger) error {
    14  	if len(list) == 0 {
    15  		return nil
    16  	}
    17  
    18  	masked := true
    19  	defer func() {
    20  		// Only log out the error if we're masking the original error, that
    21  		// way you can at least find the issue in `debug-log`.
    22  		// We do this because the original error message can be huge and
    23  		// verbose, like a java stack trace!
    24  		if masked {
    25  			logger.Errorf("charmhub API error %s:%s", list[0].Code, list[0].Message)
    26  		}
    27  	}()
    28  
    29  	switch list[0].Code {
    30  	case transport.ErrorCodeNotFound:
    31  		return errors.NotFoundf("charm or bundle")
    32  	case transport.ErrorCodeNameNotFound:
    33  		return errors.NotFoundf("charm or bundle name")
    34  	case transport.ErrorCodeResourceNotFound:
    35  		return errors.NotFoundf("charm resource")
    36  	case transport.ErrorCodeAPIError:
    37  		return errors.Errorf("unexpected api error attempting to query charm or bundle from the charmhub store")
    38  	case transport.ErrorCodeBadArgument:
    39  		return errors.BadRequestf("query argument")
    40  	}
    41  	// We haven't handled the errors, so just return them.
    42  	masked = false
    43  	return list
    44  }