github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/chat/storage/errors.go (about)

     1  package storage
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/keybase/client/go/chat/utils"
     8  	"github.com/keybase/client/go/protocol/chat1"
     9  )
    10  
    11  type Error interface {
    12  	error
    13  	ShouldClear() bool
    14  	Message() string
    15  }
    16  
    17  type InternalError struct {
    18  	Msg string
    19  }
    20  
    21  func (e InternalError) ShouldClear() bool {
    22  	return true
    23  }
    24  
    25  func (e InternalError) Error() string {
    26  	return fmt.Sprintf("internal chat storage error: %s", e.Msg)
    27  }
    28  
    29  func (e InternalError) Message() string {
    30  	return e.Msg
    31  }
    32  
    33  func NewInternalError(ctx context.Context, d utils.DebugLabeler, msg string, args ...interface{}) InternalError {
    34  	d.Debug(ctx, "internal chat storage error: "+msg, args...)
    35  	return InternalError{Msg: fmt.Sprintf(msg, args...)}
    36  }
    37  
    38  type MissError struct {
    39  	Msg string
    40  }
    41  
    42  func (e MissError) Error() string {
    43  	if len(e.Msg) > 0 {
    44  		return "chat cache miss: " + e.Msg
    45  	}
    46  	return "chat cache miss"
    47  }
    48  
    49  func (e MissError) ShouldClear() bool {
    50  	return false
    51  }
    52  
    53  func (e MissError) Message() string {
    54  	return e.Error()
    55  }
    56  
    57  type RemoteError struct {
    58  	Msg string
    59  }
    60  
    61  func (e RemoteError) Error() string {
    62  	return fmt.Sprintf("chat remote error: %s", e.Msg)
    63  }
    64  
    65  func (e RemoteError) ShouldClear() bool {
    66  	return false
    67  }
    68  
    69  func (e RemoteError) Message() string {
    70  	return e.Msg
    71  }
    72  
    73  type MiscError struct {
    74  	Msg string
    75  }
    76  
    77  func (e MiscError) Error() string {
    78  	return e.Msg
    79  }
    80  
    81  func (e MiscError) ShouldClear() bool {
    82  	return false
    83  }
    84  
    85  func (e MiscError) Message() string {
    86  	return e.Msg
    87  }
    88  
    89  type VersionMismatchError struct {
    90  	old, new chat1.InboxVers
    91  }
    92  
    93  func NewVersionMismatchError(oldVers chat1.InboxVers, newVers chat1.InboxVers) VersionMismatchError {
    94  	return VersionMismatchError{
    95  		old: oldVers,
    96  		new: newVers,
    97  	}
    98  }
    99  
   100  func (e VersionMismatchError) Error() string {
   101  	return fmt.Sprintf("version mismatch error: old %d new: %d", e.old, e.new)
   102  }
   103  
   104  func (e VersionMismatchError) ShouldClear() bool {
   105  	return false
   106  }
   107  
   108  func (e VersionMismatchError) Message() string {
   109  	return e.Error()
   110  }
   111  
   112  type AbortedError struct{}
   113  
   114  func NewAbortedError() AbortedError {
   115  	return AbortedError{}
   116  }
   117  
   118  func (e AbortedError) Error() string {
   119  	return "request aborted"
   120  }
   121  
   122  func (e AbortedError) ShouldClear() bool {
   123  	return false
   124  }
   125  
   126  func (e AbortedError) Message() string {
   127  	return e.Error()
   128  }
   129  
   130  func isAbortedRequest(ctx context.Context) Error {
   131  	// Check context for aborted request
   132  	select {
   133  	case <-ctx.Done():
   134  		return NewAbortedError()
   135  	default:
   136  	}
   137  	return nil
   138  }