github.com/oam-dev/kubevela@v1.9.11/pkg/addon/error.go (about)

     1  /*
     2  Copyright 2021 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package addon
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/google/go-github/v32/github"
    23  	"github.com/pkg/errors"
    24  )
    25  
    26  // NewAddonError will return an
    27  func NewAddonError(msg string) error {
    28  	return errors.New(msg)
    29  }
    30  
    31  var (
    32  	// ErrRenderCueTmpl is error when render addon's cue file
    33  	ErrRenderCueTmpl = NewAddonError("fail to render cue tmpl")
    34  
    35  	// ErrRateLimit means exceed GitHub access rate limit
    36  	ErrRateLimit = NewAddonError("exceed github access rate limit")
    37  
    38  	// ErrNotExist  means addon not exists
    39  	ErrNotExist = NewAddonError("addon not exist")
    40  
    41  	// ErrRegistryNotExist means registry not exists
    42  	ErrRegistryNotExist = NewAddonError("registry does not exist")
    43  
    44  	// ErrBothCueAndYamlTmpl means yaml and cue app template are exist in addon
    45  	ErrBothCueAndYamlTmpl = NewAddonError("yaml and cue app template are exist in addon, should only keep one of them")
    46  
    47  	// ErrFetch means fetch addon package error(package not exist or parse archive error and so on)
    48  	ErrFetch = NewAddonError("cannot fetch addon package")
    49  )
    50  
    51  // WrapErrRateLimit return ErrRateLimit if is the situation, or return error directly
    52  func WrapErrRateLimit(err error) error {
    53  	errRate := &github.RateLimitError{}
    54  	if errors.As(err, &errRate) {
    55  		return ErrRateLimit
    56  	}
    57  	return err
    58  }
    59  
    60  // VersionUnMatchError means addon system requirement cannot meet requirement
    61  type VersionUnMatchError struct {
    62  	err       error
    63  	addonName string
    64  	// userSelectedAddonVersion is the version of the addon which is selected to install by user
    65  	userSelectedAddonVersion string
    66  	// availableVersion is the latest available addon's version which suits system requirements
    67  	availableVersion string
    68  }
    69  
    70  // GetAvailableVersion load addon's available version from the err
    71  func (v VersionUnMatchError) GetAvailableVersion() (string, error) {
    72  	if v.availableVersion == "" {
    73  		return "", fmt.Errorf("%s don't exist available version meet system requirement", v.addonName)
    74  	}
    75  	return v.availableVersion, nil
    76  }
    77  
    78  func (v VersionUnMatchError) Error() string {
    79  	if v.availableVersion != "" {
    80  		return fmt.Sprintf("fail to install %s version of %s, because %s.\nInstall %s(v%s) which is the latest version that suits current version requirements", v.userSelectedAddonVersion, v.addonName, v.err, v.addonName, v.availableVersion)
    81  	}
    82  	return fmt.Sprintf("fail to install %s version of %s, because %s", v.userSelectedAddonVersion, v.addonName, v.err)
    83  
    84  }