github.com/cyverse/go-irodsclient@v0.13.2/irods/types/error.go (about)

     1  package types
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/cyverse/go-irodsclient/irods/common"
     8  )
     9  
    10  // ConnectionConfigError contains connection config error information
    11  type ConnectionConfigError struct {
    12  	Config *IRODSAccount
    13  }
    14  
    15  // NewConnectionConfigError creates a connection config error
    16  func NewConnectionConfigError(config *IRODSAccount) error {
    17  	return &ConnectionConfigError{
    18  		Config: config.GetRedacted(),
    19  	}
    20  }
    21  
    22  // Error returns error message
    23  func (err *ConnectionConfigError) Error() string {
    24  	return fmt.Sprintf("connection configuration error (iRODS server: '%s:%d')", err.Config.Host, err.Config.Port)
    25  }
    26  
    27  // Is tests type of error
    28  func (err *ConnectionConfigError) Is(other error) bool {
    29  	_, ok := other.(*ConnectionConfigError)
    30  	return ok
    31  }
    32  
    33  // ToString stringifies the object
    34  func (err *ConnectionConfigError) ToString() string {
    35  	return "<ConnectionConfigError>"
    36  }
    37  
    38  // IsConnectionConfigError evaluates if the given error is connection config error
    39  func IsConnectionConfigError(err error) bool {
    40  	return errors.Is(err, &ConnectionConfigError{})
    41  }
    42  
    43  // ConnectionError contains connection error information
    44  type ConnectionError struct {
    45  }
    46  
    47  // NewConnectionError creates an error for connection poll full
    48  func NewConnectionError() error {
    49  	return &ConnectionError{}
    50  }
    51  
    52  // Error returns error message
    53  func (err *ConnectionError) Error() string {
    54  	return "connection error"
    55  }
    56  
    57  // Is tests type of error
    58  func (err *ConnectionError) Is(other error) bool {
    59  	_, ok := other.(*ConnectionError)
    60  	return ok
    61  }
    62  
    63  // ToString stringifies the object
    64  func (err *ConnectionError) ToString() string {
    65  	return "<ConnectionError>"
    66  }
    67  
    68  // IsConnectionError evaluates if the given error is connection error
    69  func IsConnectionError(err error) bool {
    70  	return errors.Is(err, &ConnectionError{})
    71  }
    72  
    73  // AuthError contains auth error information
    74  type AuthError struct {
    75  	Config *IRODSAccount
    76  }
    77  
    78  // NewAuthError creates an error for auth
    79  func NewAuthError(config *IRODSAccount) error {
    80  	return &AuthError{
    81  		Config: config.GetRedacted(),
    82  	}
    83  }
    84  
    85  // Error returns error message
    86  func (err *AuthError) Error() string {
    87  	return fmt.Sprintf("authentication error (auth scheme: '%s', username: '%s', zone: '%s')", err.Config.AuthenticationScheme, err.Config.ClientUser, err.Config.ClientZone)
    88  }
    89  
    90  // Is tests type of error
    91  func (err *AuthError) Is(other error) bool {
    92  	_, ok := other.(*AuthError)
    93  	return ok
    94  }
    95  
    96  // ToString stringifies the object
    97  func (err *AuthError) ToString() string {
    98  	return "<AuthError>"
    99  }
   100  
   101  // IsAuthError evaluates if the given error is authentication failure
   102  func IsAuthError(err error) bool {
   103  	return errors.Is(err, &AuthError{})
   104  }
   105  
   106  // ConnectionPoolFullError contains connection pool full error information
   107  type ConnectionPoolFullError struct {
   108  	Occupied int
   109  	Max      int
   110  }
   111  
   112  // NewConnectionPoolFullError creates an error for connection poll full
   113  func NewConnectionPoolFullError(requested int, max int) error {
   114  	return &ConnectionPoolFullError{
   115  		Occupied: requested,
   116  		Max:      requested,
   117  	}
   118  }
   119  
   120  // Error returns error message
   121  func (err *ConnectionPoolFullError) Error() string {
   122  	return fmt.Sprintf("connection pool is full (occupied: %d, max: %d)", err.Occupied, err.Max)
   123  }
   124  
   125  // Is tests type of error
   126  func (err *ConnectionPoolFullError) Is(other error) bool {
   127  	_, ok := other.(*ConnectionPoolFullError)
   128  	return ok
   129  }
   130  
   131  // ToString stringifies the object
   132  func (err *ConnectionPoolFullError) ToString() string {
   133  	return "<ConnectionPoolFullError>"
   134  }
   135  
   136  // IsConnectionPoolFullError evaluates if the given error is connection full error
   137  func IsConnectionPoolFullError(err error) bool {
   138  	return errors.Is(err, &ConnectionPoolFullError{})
   139  }
   140  
   141  // CollectionNotEmptyError contains collection not empty error information
   142  type CollectionNotEmptyError struct {
   143  	Path string
   144  }
   145  
   146  // NewCollectionNotEmptyError creates an error for collection not empty
   147  func NewCollectionNotEmptyError(p string) error {
   148  	return &CollectionNotEmptyError{
   149  		Path: p,
   150  	}
   151  }
   152  
   153  // Error returns error message
   154  func (err *CollectionNotEmptyError) Error() string {
   155  	return fmt.Sprintf("collection not empty for path %s", err.Path)
   156  }
   157  
   158  // Is tests type of error
   159  func (err *CollectionNotEmptyError) Is(other error) bool {
   160  	_, ok := other.(*CollectionNotEmptyError)
   161  	return ok
   162  }
   163  
   164  // ToString stringifies the object
   165  func (err *CollectionNotEmptyError) ToString() string {
   166  	return fmt.Sprintf("<CollectionNotEmptyError %s>", err.Path)
   167  }
   168  
   169  // IsCollectionNotEmptyError evaluates if the given error is collection not empty error
   170  func IsCollectionNotEmptyError(err error) bool {
   171  	return errors.Is(err, &CollectionNotEmptyError{})
   172  }
   173  
   174  // FileNotFoundError contains file not found error information
   175  type FileNotFoundError struct {
   176  	Path string
   177  }
   178  
   179  // NewFileNotFoundError creates an error for file not found
   180  func NewFileNotFoundError(p string) error {
   181  	return &FileNotFoundError{
   182  		Path: p,
   183  	}
   184  }
   185  
   186  // Error returns error message
   187  func (err *FileNotFoundError) Error() string {
   188  	return fmt.Sprintf("data object/collection not found for path %s", err.Path)
   189  }
   190  
   191  // Is tests type of error
   192  func (err *FileNotFoundError) Is(other error) bool {
   193  	_, ok := other.(*FileNotFoundError)
   194  	return ok
   195  }
   196  
   197  // ToString stringifies the object
   198  func (err *FileNotFoundError) ToString() string {
   199  	return fmt.Sprintf("<FileNotFoundError %s>", err.Path)
   200  }
   201  
   202  // IsFileNotFoundError checks if the given error is FileNotFoundError
   203  func IsFileNotFoundError(err error) bool {
   204  	return errors.Is(err, &FileNotFoundError{})
   205  }
   206  
   207  // FileAlreadyExistError contains file already exist error information
   208  type FileAlreadyExistError struct {
   209  	Path string
   210  }
   211  
   212  // NewFileAlreadyExistError creates an error for file already exist
   213  func NewFileAlreadyExistError(p string) error {
   214  	return &FileAlreadyExistError{
   215  		Path: p,
   216  	}
   217  }
   218  
   219  // Error returns error message
   220  func (err *FileAlreadyExistError) Error() string {
   221  	return fmt.Sprintf("data object/collection already exist for path %s", err.Path)
   222  }
   223  
   224  // Is tests type of error
   225  func (err *FileAlreadyExistError) Is(other error) bool {
   226  	_, ok := other.(*FileAlreadyExistError)
   227  	return ok
   228  }
   229  
   230  // ToString stringifies the object
   231  func (err *FileAlreadyExistError) ToString() string {
   232  	return fmt.Sprintf("<FileAlreadyExistError %s>", err.Path)
   233  }
   234  
   235  // IsFileAlreadyExistError checks if the given error is FileAlreadyExistError
   236  func IsFileAlreadyExistError(err error) bool {
   237  	return errors.Is(err, &FileAlreadyExistError{})
   238  }
   239  
   240  // TicketNotFoundError contains ticket not found error information
   241  type TicketNotFoundError struct {
   242  	Ticket string
   243  }
   244  
   245  // NewTicketNotFoundError creates an error for ticket not found
   246  func NewTicketNotFoundError(ticket string) error {
   247  	return &TicketNotFoundError{
   248  		Ticket: ticket,
   249  	}
   250  }
   251  
   252  // Error returns error message
   253  func (err *TicketNotFoundError) Error() string {
   254  	return fmt.Sprintf("ticket %s not found", err.Ticket)
   255  }
   256  
   257  // Is tests type of error
   258  func (err *TicketNotFoundError) Is(other error) bool {
   259  	_, ok := other.(*TicketNotFoundError)
   260  	return ok
   261  }
   262  
   263  // ToString stringifies the object
   264  func (err *TicketNotFoundError) ToString() string {
   265  	return fmt.Sprintf("<TicketNotFoundError %s>", err.Ticket)
   266  }
   267  
   268  // IsTicketNotFoundError checks if the given error is TicketNotFoundError
   269  func IsTicketNotFoundError(err error) bool {
   270  	return errors.Is(err, &TicketNotFoundError{})
   271  }
   272  
   273  // UserNotFoundError contains user/group not found error information
   274  type UserNotFoundError struct {
   275  	Name string
   276  }
   277  
   278  // NewUserNotFoundError creates an error for user not found
   279  func NewUserNotFoundError(name string) error {
   280  	return &UserNotFoundError{
   281  		Name: name,
   282  	}
   283  }
   284  
   285  // Error returns error message
   286  func (err *UserNotFoundError) Error() string {
   287  	return fmt.Sprintf("user/group %s not found", err.Name)
   288  }
   289  
   290  // Is tests type of error
   291  func (err *UserNotFoundError) Is(other error) bool {
   292  	_, ok := other.(*UserNotFoundError)
   293  	return ok
   294  }
   295  
   296  // ToString stringifies the object
   297  func (err *UserNotFoundError) ToString() string {
   298  	return fmt.Sprintf("<UserNotFoundError %s>", err.Name)
   299  }
   300  
   301  // IsUserNotFoundError checks if the given error is UserNotFoundError
   302  func IsUserNotFoundError(err error) bool {
   303  	return errors.Is(err, &UserNotFoundError{})
   304  }
   305  
   306  // IRODSError contains irods error information
   307  type IRODSError struct {
   308  	Code              common.ErrorCode
   309  	Message           string
   310  	ContextualMessage string
   311  }
   312  
   313  // NewIRODSError creates a new IRODSError
   314  func NewIRODSError(code common.ErrorCode) *IRODSError {
   315  	return &IRODSError{
   316  		Code:              code,
   317  		Message:           common.GetIRODSErrorString(code),
   318  		ContextualMessage: "",
   319  	}
   320  }
   321  
   322  // NewIRODSErrorWithString creates a new IRODSError with message
   323  func NewIRODSErrorWithString(code common.ErrorCode, message string) *IRODSError {
   324  	return &IRODSError{
   325  		Code:              code,
   326  		Message:           common.GetIRODSErrorString(code),
   327  		ContextualMessage: message,
   328  	}
   329  }
   330  
   331  // Error returns error message
   332  func (err *IRODSError) Error() string {
   333  	if len(err.ContextualMessage) > 0 {
   334  		return fmt.Sprintf("%s - %s", err.Message, err.ContextualMessage)
   335  	}
   336  	return err.Message
   337  }
   338  
   339  // Is tests type of error
   340  func (err *IRODSError) Is(other error) bool {
   341  	_, ok := other.(*IRODSError)
   342  	return ok
   343  }
   344  
   345  // GetCode returns error code
   346  func (err *IRODSError) GetCode() common.ErrorCode {
   347  	return err.Code
   348  }
   349  
   350  // ToString stringifies the object
   351  func (err *IRODSError) ToString() string {
   352  	return fmt.Sprintf("<IRODSError %d %s %s>", err.Code, err.Message, err.ContextualMessage)
   353  }
   354  
   355  // IsIRODSError checks if the given error is IRODSError
   356  func IsIRODSError(err error) bool {
   357  	return errors.Is(err, &IRODSError{})
   358  }
   359  
   360  // GetIRODSErrorCode returns iRODS error code if the error is iRODSError
   361  func GetIRODSErrorCode(err error) common.ErrorCode {
   362  	if err == nil {
   363  		return common.ErrorCode(0)
   364  	}
   365  
   366  	var irodsError *IRODSError
   367  	if errors.As(err, &irodsError) {
   368  		return irodsError.GetCode()
   369  	}
   370  	return common.ErrorCode(0)
   371  }
   372  
   373  // IsPermanantFailure returns if given error is permanant failure
   374  func IsPermanantFailure(err error) bool {
   375  	if err == nil {
   376  		return false
   377  	}
   378  
   379  	if IsAuthError(err) {
   380  		return true
   381  	} else if IsConnectionConfigError(err) {
   382  		return true
   383  	} else if IsConnectionError(err) {
   384  		return false
   385  	} else if IsConnectionPoolFullError(err) {
   386  		return false
   387  	}
   388  
   389  	return false
   390  }