github.com/blend/go-sdk@v1.20220411.3/configutil/errors.go (about)

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