...

Source file src/github.com/launchdarkly/go-jsonstream/v3/jreader/errors.go

Documentation: github.com/launchdarkly/go-jsonstream/v3/jreader

     1  package jreader
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"reflect"
     7  )
     8  
     9  const (
    10  	errMsgBadArrayItem     = "expected comma or end of array"
    11  	errMsgBadObjectItem    = "expected comma or end of object"
    12  	errMsgDataAfterEnd     = "unexpected data after end of JSON value"
    13  	errMsgExpectedColon    = "expected colon after property name"
    14  	errMsgInvalidNumber    = "invalid numeric value"
    15  	errMsgInvalidString    = "unterminated or invalid string value"
    16  	errMsgUnexpectedChar   = "unexpected character"
    17  	errMsgUnexpectedSymbol = "unexpected symbol"
    18  )
    19  
    20  // SyntaxError is returned by Reader if the input is not well-formed JSON.
    21  type SyntaxError struct {
    22  	// Message is a descriptive message.
    23  	Message string
    24  
    25  	// Offset is the approximate character index within the input where the error occurred.
    26  	Offset int
    27  
    28  	// Value, if not empty, is the token that caused the error.
    29  	Value string
    30  }
    31  
    32  // TypeError is returned by Reader if the type of JSON value that was read did not
    33  // match what the caller requested.
    34  type TypeError struct {
    35  	// Expected is the type of JSON value that the caller requested.
    36  	Expected ValueKind
    37  
    38  	// Actual is the type of JSON value that was found.
    39  	Actual ValueKind
    40  
    41  	// Nullable is true if the caller indicated that a null value was acceptable in this context.
    42  	Nullable bool
    43  
    44  	// Offset is the approximate character index within the input where the error occurred.
    45  	Offset int
    46  }
    47  
    48  // RequiredPropertyError is returned by Reader if a JSON object did not contain a property that
    49  // was designated as required (by using ObjectState.WithRequiredProperties).
    50  type RequiredPropertyError struct {
    51  	// Name is the name of a required object property that was not found.
    52  	Name string
    53  
    54  	// Offset is the approximate character index within the input where the error occurred
    55  	// (at or near the end of the JSON object).
    56  	Offset int
    57  }
    58  
    59  // Error returns a description of the error.
    60  func (e SyntaxError) Error() string {
    61  	if e.Value != "" {
    62  		return fmt.Sprintf("%s at position %d (%q)", e.Message, e.Offset, e.Value)
    63  	}
    64  	return fmt.Sprintf("%s at position %d", e.Message, e.Offset)
    65  }
    66  
    67  // Error returns a description of the error.
    68  func (e TypeError) Error() string {
    69  	if e.Nullable {
    70  		return fmt.Sprintf("expected %s or null, got %s at position %d", e.Expected, e.Actual, e.Offset)
    71  	}
    72  	return fmt.Sprintf("expected %s, got %s at position %d", e.Expected, e.Actual, e.Offset)
    73  }
    74  
    75  // Error returns a description of the error.
    76  func (e RequiredPropertyError) Error() string {
    77  	return fmt.Sprintf("a required property %q was missing from a JSON object at position %d", e.Name, e.Offset)
    78  }
    79  
    80  // ToJSONError converts errors defined by the jreader package into the corresponding error types defined
    81  // by the encoding/json package, if any. The target parameter, if not nil, is used to determine the
    82  // target value type for json.UnmarshalTypeError.
    83  func ToJSONError(err error, target interface{}) error {
    84  	switch e := err.(type) {
    85  	case SyntaxError:
    86  		return &json.SyntaxError{
    87  			Offset: int64(e.Offset),
    88  		}
    89  	case TypeError:
    90  		return &json.UnmarshalTypeError{
    91  			Value:  e.Expected.String(),
    92  			Type:   reflect.TypeOf(target),
    93  			Offset: int64(e.Offset),
    94  		}
    95  	}
    96  	return err
    97  }
    98  

View as plain text