github.com/viant/toolbox@v0.34.5/error.go (about)

     1  package toolbox
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  )
     8  
     9  //NilPointerError represents nil pointer error
    10  type NilPointerError struct {
    11  	message string
    12  }
    13  
    14  //Error returns en error
    15  func (e *NilPointerError) Error() string {
    16  	if e.message == "" {
    17  		return "NilPointerError"
    18  	}
    19  	return e.message
    20  }
    21  
    22  //NewNilPointerError creates a new nil pointer error
    23  func NewNilPointerError(message string) error {
    24  	return &NilPointerError{
    25  		message: message,
    26  	}
    27  }
    28  
    29  //IsNilPointerError returns true if error is nil pointer
    30  func IsNilPointerError(err error) bool {
    31  	if err == nil {
    32  		return false
    33  	}
    34  	_, ok := err.(*NilPointerError)
    35  	return ok
    36  }
    37  
    38  //IsEOFError returns true if io.EOF
    39  func IsEOFError(err error) bool {
    40  	if err == nil {
    41  		return false
    42  	}
    43  	return err == io.EOF
    44  }
    45  
    46  //NotFoundError represents not found error
    47  type NotFoundError struct {
    48  	URL string
    49  }
    50  
    51  func (e *NotFoundError) Error() string {
    52  	return fmt.Sprintf("not found: %v", e.URL)
    53  }
    54  
    55  //IsNotFoundError checks is supplied error is NotFoundError type
    56  func IsNotFoundError(err error) bool {
    57  	if err == nil {
    58  		return false
    59  	}
    60  	_, ok := err.(*NotFoundError)
    61  	return ok
    62  }
    63  
    64  //ReclassifyNotFoundIfMatched reclassify error if not found
    65  func ReclassifyNotFoundIfMatched(err error, URL string) error {
    66  	if err == nil {
    67  		return nil
    68  	}
    69  	message := strings.ToLower(err.Error())
    70  	if strings.Contains(message, "doesn't exist") ||
    71  		strings.Contains(message, "no such file or directory") ||
    72  		strings.Contains(err.Error(), "404") ||
    73  		strings.Contains(err.Error(), "nosuchbucket") {
    74  		return &NotFoundError{URL: URL}
    75  	}
    76  	return err
    77  }