Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export errors #302

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions jsonapi/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package jsonapi

import "errors"

//Error for all errors within this package
type Error interface {
error
}

//MarshalError interface
type MarshalError interface {
Error
}

//UnmarshalError interface
type UnmarshalError interface {
Error
}

// Marshal errors
var (
// ErrInvalidMarshalType is returned if marshal is called with an invalid type
ErrInvalidMarshalType MarshalError = errors.New("Marshal only accepts slice, struct or ptr types")
// ErrNonHomogenousSlice if any element within a slice does not implement marshal identifier
ErrNonHomogenousSlice MarshalError = errors.New("all elements within the slice must implement api2go.MarshalIdentifier")
//ErrNonNilElement marshaling a nil type is not possible
ErrNonNilElement MarshalError = errors.New("MarshalIdentifier must not be nil")
)

// Unmarshal errors
var (
// ErrNilTarget if element will be unmarshalled into a nil element
ErrNilTarget UnmarshalError = errors.New("target must not be nil")
// ErrNonPointerTarget
ErrNonPointerTarget UnmarshalError = errors.New("target must be a ptr")
)

// Semantic errors
var (
ErrInvalidUnmarshalType UnmarshalError = errors.New("target must implement UnmarshalIdentifier interface")
ErrEmptySource UnmarshalError = errors.New(`Source JSON is empty and has no "attributes" payload object`)
ErrMissingType UnmarshalError = errors.New("invalid record, no type was specified")
ErrInvalidStruct UnmarshalError = errors.New("existing structs must implement interface MarshalIdentifier")
)
7 changes: 3 additions & 4 deletions jsonapi/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jsonapi

import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -132,7 +131,7 @@ func MarshalToStruct(data interface{}, information ServerInformation) (*Document
case reflect.Struct, reflect.Ptr:
return marshalStruct(data.(MarshalIdentifier), information)
default:
return nil, errors.New("Marshal only accepts slice, struct or ptr types")
return nil, ErrInvalidMarshalType
}
}

Expand Down Expand Up @@ -168,7 +167,7 @@ func marshalSlice(data interface{}, information ServerInformation) (*Document, e
k := val.Index(i).Interface()
element, ok := k.(MarshalIdentifier)
if !ok {
return nil, errors.New("all elements within the slice must implement api2go.MarshalIdentifier")
return nil, ErrNonHomogenousSlice
}

err := marshalData(element, &dataElements[i], information)
Expand Down Expand Up @@ -228,7 +227,7 @@ func filterDuplicates(input []MarshalIdentifier, information ServerInformation)
func marshalData(element MarshalIdentifier, data *Data, information ServerInformation) error {
refValue := reflect.ValueOf(element)
if refValue.Kind() == reflect.Ptr && refValue.IsNil() {
return errors.New("MarshalIdentifier must not be nil")
return ErrNonNilElement
}

attributes, err := json.Marshal(element)
Expand Down
14 changes: 7 additions & 7 deletions jsonapi/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jsonapi

import (
"encoding/json"
"errors"
"fmt"
"reflect"
)
Expand Down Expand Up @@ -80,11 +79,11 @@ type EditToManyRelations interface {
// must implement the `UnmarshalIdentifier` interface.
func Unmarshal(data []byte, target interface{}) error {
if target == nil {
return errors.New("target must not be nil")
return ErrNilTarget
}

if reflect.TypeOf(target).Kind() != reflect.Ptr {
return errors.New("target must be a ptr")
return ErrNonPointerTarget
}

ctx := &Document{}
Expand All @@ -95,7 +94,7 @@ func Unmarshal(data []byte, target interface{}) error {
}

if ctx.Data == nil {
return errors.New(`Source JSON is empty and has no "attributes" payload object`)
return ErrEmptySource
}

if ctx.Data.DataObject != nil {
Expand All @@ -118,8 +117,9 @@ func Unmarshal(data []byte, target interface{}) error {
for i := 0; i < targetValue.Len(); i++ {
marshalCasted, ok := targetValue.Index(i).Interface().(MarshalIdentifier)
if !ok {
return errors.New("existing structs must implement interface MarshalIdentifier")
return ErrInvalidStruct
}

if record.ID == marshalCasted.GetID() {
targetRecord = targetValue.Index(i).Addr()
break
Expand Down Expand Up @@ -150,11 +150,11 @@ func Unmarshal(data []byte, target interface{}) error {
func setDataIntoTarget(data *Data, target interface{}) error {
castedTarget, ok := target.(UnmarshalIdentifier)
if !ok {
return errors.New("target must implement UnmarshalIdentifier interface")
return ErrInvalidUnmarshalType
}

if data.Type == "" {
return errors.New("invalid record, no type was specified")
return ErrMissingType
}

err := checkType(data.Type, castedTarget)
Expand Down