github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/store/errors.go (about)

     1  package store
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // ErrInvalidInput indicates an error that has occurred due to an invalid input.
     8  type ErrInvalidInput struct {
     9  	Entity string      // The entity which was sent as the input.
    10  	Field  string      // The field of the entity which was invalid.
    11  	Value  interface{} // The actual value of the field.
    12  }
    13  
    14  func NewErrInvalidInput(entity, field string, value interface{}) *ErrInvalidInput {
    15  	return &ErrInvalidInput{
    16  		Entity: entity,
    17  		Field:  field,
    18  		Value:  value,
    19  	}
    20  }
    21  
    22  func (e *ErrInvalidInput) Error() string {
    23  	return fmt.Sprintf("invalid input: entity: %s field: %s value: %s", e.Entity, e.Field, e.Value)
    24  }
    25  
    26  func (e *ErrInvalidInput) InvalidInputInfo() (entity string, field string, value interface{}) {
    27  	entity = e.Entity
    28  	field = e.Field
    29  	value = e.Value
    30  	return
    31  }
    32  
    33  // ErrLimitExceeded indicates an error that has occurred because some value exceeded a limit.
    34  type ErrLimitExceeded struct {
    35  	What  string // What was the object that exceeded.
    36  	Count int    // The value of the object.
    37  	meta  string // Any additional metadata.
    38  }
    39  
    40  func NewErrLimitExceeded(what string, count int, meta string) *ErrLimitExceeded {
    41  	return &ErrLimitExceeded{
    42  		What:  what,
    43  		Count: count,
    44  		meta:  meta,
    45  	}
    46  }
    47  
    48  func (e *ErrLimitExceeded) Error() string {
    49  	return fmt.Sprintf("limit exceeded: what: %s count: %d metadata: %s", e.What, e.Count, e.meta)
    50  }
    51  
    52  // ErrConflict indicates a conflict that occurred.
    53  type ErrConflict struct {
    54  	Resource string // The resource which created the conflict.
    55  	err      error  // Internal error.
    56  	meta     string // Any additional metadata.
    57  }
    58  
    59  func NewErrConflict(resource string, err error, meta string) *ErrConflict {
    60  	return &ErrConflict{
    61  		Resource: resource,
    62  		err:      err,
    63  		meta:     meta,
    64  	}
    65  }
    66  
    67  func (e *ErrConflict) Error() string {
    68  	msg := e.Resource + "exists " + e.meta
    69  	if e.err != nil {
    70  		msg += " " + e.err.Error()
    71  	}
    72  	return msg
    73  }
    74  
    75  func (e *ErrConflict) Unwrap() error {
    76  	return e.err
    77  }
    78  
    79  // IsErrConflict allows easy type assertion without adding store as a dependency.
    80  func (e *ErrConflict) IsErrConflict() bool {
    81  	return true
    82  }
    83  
    84  // ErrNotFound indicates that a resource was not found
    85  type ErrNotFound struct {
    86  	resource string
    87  	ID       string
    88  }
    89  
    90  func NewErrNotFound(resource, id string) *ErrNotFound {
    91  	return &ErrNotFound{
    92  		resource: resource,
    93  		ID:       id,
    94  	}
    95  }
    96  
    97  func (e *ErrNotFound) Error() string {
    98  	return "resource: " + e.resource + " id: " + e.ID
    99  }
   100  
   101  // IsErrNotFound allows easy type assertion without adding store as a dependency.
   102  func (e *ErrNotFound) IsErrNotFound() bool {
   103  	return true
   104  }
   105  
   106  // ErrOutOfBounds indicates that the requested total numbers of rows
   107  // was greater than the allowed limit.
   108  type ErrOutOfBounds struct {
   109  	value int
   110  }
   111  
   112  func (e *ErrOutOfBounds) Error() string {
   113  	return fmt.Sprintf("invalid limit parameter: %d", e.value)
   114  }
   115  
   116  func NewErrOutOfBounds(value int) *ErrOutOfBounds {
   117  	return &ErrOutOfBounds{value: value}
   118  }
   119  
   120  // ErrNotImplemented indicates that some feature or requirement is not implemented yet.
   121  type ErrNotImplemented struct {
   122  	detail string
   123  }
   124  
   125  func (e *ErrNotImplemented) Error() string {
   126  	return e.detail
   127  }
   128  
   129  func NewErrNotImplemented(detail string) *ErrNotImplemented {
   130  	return &ErrNotImplemented{detail: detail}
   131  }