...

Source file src/github.com/launchdarkly/go-sdk-common/v3/lderrors/context_errors.go

Documentation: github.com/launchdarkly/go-sdk-common/v3/lderrors

     1  package lderrors
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	"golang.org/x/exp/maps"
     9  )
    10  
    11  const (
    12  	msgContextUninitialized          = "tried to use uninitialized Context"
    13  	msgContextKeyEmpty               = "context key must not be empty"
    14  	msgContextKeyNull                = "context key must not be null"
    15  	msgContextKeyMissing             = `"key" property not found in JSON context object`
    16  	msgContextKindEmpty              = "context kind cannot be empty"
    17  	msgContextKindCannotBeKind       = `"kind" is not a valid context kind`
    18  	msgContextKindMultiForSingleKind = `single context cannot have the kind "multi"`
    19  	msgContextKindMultiWithNoKinds   = "multi-context must contain at least one kind"
    20  	msgContextKindMultiDuplicates    = "multi-context cannot have same kind more than once"
    21  	msgContextKindInvalidChars       = "context kind contains disallowed characters"
    22  )
    23  
    24  // ErrContextUninitialized means that you have tried to use an empty ldcontext.Context{} struct.
    25  type ErrContextUninitialized struct{}
    26  
    27  // ErrContextKeyEmpty means that the ldcontext.Context Key field was set to an empty string.
    28  type ErrContextKeyEmpty struct{}
    29  
    30  // ErrContextKeyNull means that the "key" property in the JSON representation of an ldcontext.Context
    31  // had a null value. This is specific to JSON unmarshaling, since there is no way to specify a null
    32  // value for this field programmatically.
    33  type ErrContextKeyNull struct{}
    34  
    35  // ErrContextKeyMissing means that the JSON representation of an ldcontext.Context had no "key"
    36  // property.
    37  type ErrContextKeyMissing struct{}
    38  
    39  // ErrContextKindEmpty means that the "kind" property in the JSON representation of an
    40  // ldcontext.Context had an empty string value. This is specific to JSON unmarshaling, since if you
    41  // are creating a Context programmatically, an empty string is automatically changed to
    42  // ldcontext.DefaultKind.
    43  type ErrContextKindEmpty struct{}
    44  
    45  // ErrContextKindCannotBeKind means that you have tried to set the ldcontext.Context Kind field to
    46  // the string "kind". The Context schema does not allow this.
    47  type ErrContextKindCannotBeKind struct{}
    48  
    49  // ErrContextKindMultiForSingleKind means that you have tried to set the ldcontext.Context Kind
    50  // field to the string "multi" for a single context. The Context schema does not allow this.
    51  type ErrContextKindMultiForSingleKind struct{}
    52  
    53  // ErrContextKindMultiWithNoKinds means that you have used an ldcontext constructor or builder
    54  // for a multi-context but you did not specify any individual Contexts in it.
    55  type ErrContextKindMultiWithNoKinds struct{}
    56  
    57  // ErrContextKindMultiDuplicates means that you have used an ldcontext constructor or builder
    58  // for a multi-context and you specified more than one individual Context in it with the
    59  // same kind.
    60  type ErrContextKindMultiDuplicates struct{}
    61  
    62  // ErrContextKindInvalidChars means that you have tried to set the ldcontext.Context Kind field to
    63  // a string that contained disallowed characters.
    64  type ErrContextKindInvalidChars struct{}
    65  
    66  // ErrContextPerKindErrors means that a multi-context contained at least one kind where the
    67  // individual Context was invalid. There may be a separate validation error for each kind.
    68  type ErrContextPerKindErrors struct {
    69  	// Errors is a map where each key is the context kind (as a string) and the value is the
    70  	// validation error for that kind.
    71  	Errors map[string]error
    72  }
    73  
    74  func (e ErrContextUninitialized) Error() string          { return msgContextUninitialized }
    75  func (e ErrContextKeyEmpty) Error() string               { return msgContextKeyEmpty }
    76  func (e ErrContextKeyNull) Error() string                { return msgContextKeyNull }
    77  func (e ErrContextKeyMissing) Error() string             { return msgContextKeyMissing }
    78  func (e ErrContextKindEmpty) Error() string              { return msgContextKindEmpty }
    79  func (e ErrContextKindCannotBeKind) Error() string       { return msgContextKindCannotBeKind }
    80  func (e ErrContextKindMultiForSingleKind) Error() string { return msgContextKindMultiForSingleKind }
    81  func (e ErrContextKindMultiWithNoKinds) Error() string   { return msgContextKindMultiWithNoKinds }
    82  func (e ErrContextKindMultiDuplicates) Error() string    { return msgContextKindMultiDuplicates }
    83  func (e ErrContextKindInvalidChars) Error() string       { return msgContextKindInvalidChars }
    84  
    85  func (e ErrContextPerKindErrors) Error() string {
    86  	sortedKeys := maps.Keys(e.Errors)
    87  	sort.Strings(sortedKeys)
    88  	messages := make([]string, 0, len(e.Errors))
    89  	for _, kind := range sortedKeys {
    90  		messages = append(messages, fmt.Sprintf("(%s) %s", kind, e.Errors[kind]))
    91  	}
    92  	return strings.Join(messages, ", ")
    93  }
    94  

View as plain text