go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/configutil/errors.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package configutil
     9  
    10  import (
    11  	"errors"
    12  	"os"
    13  )
    14  
    15  var (
    16  	// ErrConfigPathUnset is a common error.
    17  	ErrConfigPathUnset = errors.New("config path unset")
    18  
    19  	// ErrInvalidConfigExtension is a common error.
    20  	ErrInvalidConfigExtension = errors.New("config extension invalid")
    21  )
    22  
    23  // IsIgnored returns if we should ignore the config read error.
    24  func IsIgnored(err error) bool {
    25  	if err == nil {
    26  		return true
    27  	}
    28  	if IsNotExist(err) || IsConfigPathUnset(err) || IsInvalidConfigExtension(err) {
    29  		return true
    30  	}
    31  	return false
    32  }
    33  
    34  // IsNotExist returns if an error is an os.ErrNotExist.
    35  //
    36  // Read will never return a not found error, instead it will
    37  // simply skip over that file, `IsNotExist` should be used
    38  // in other situations like in resolvers.
    39  func IsNotExist(err error) bool {
    40  	if err == nil {
    41  		return false
    42  	}
    43  	return os.IsNotExist(err)
    44  }
    45  
    46  // IsConfigPathUnset returns if an error is an ErrConfigPathUnset.
    47  func IsConfigPathUnset(err error) bool {
    48  	return errors.Is(err, ErrConfigPathUnset)
    49  }
    50  
    51  // IsInvalidConfigExtension returns if an error is an ErrInvalidConfigExtension.
    52  func IsInvalidConfigExtension(err error) bool {
    53  	return errors.Is(err, ErrInvalidConfigExtension)
    54  }