Unified error messages when being unable to convert to specific types using OnXXX helpers

This commit is contained in:
mariusor 2022-05-25 13:36:56 +02:00
parent 7c011e3a74
commit a9858c9098
No known key found for this signature in database
GPG key ID: DBF5E47F5DBC4D21
16 changed files with 33 additions and 33 deletions

View file

@ -3,7 +3,7 @@ package activitypub
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"reflect"
"strings"
"time"
@ -758,7 +758,7 @@ func ToActivity(it Item) (*Activity, error) {
}
}
}
return nil, errors.New("unable to convert activity")
return nil, fmt.Errorf("unable to convert %T to activity", it)
}
// MarshalJSON encodes the receiver object to a JSON document.

View file

@ -3,7 +3,7 @@ package activitypub
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"reflect"
"time"
"unsafe"
@ -336,7 +336,7 @@ func ToCollection(it Item) (*Collection, error) {
}
}
}
return nil, errors.New("unable to convert to collection")
return nil, fmt.Errorf("unable to convert %T to collection", it)
}
// FollowingNew initializes a new Following

View file

@ -3,7 +3,7 @@ package activitypub
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"reflect"
"time"
@ -340,7 +340,7 @@ func ToCollectionPage(it Item) (*CollectionPage, error) {
}
}
}
return nil, errors.New("unable to convert to collection page")
return nil, fmt.Errorf("unable to convert %T to collection page", it)
}
// ItemsMatch

View file

@ -138,7 +138,7 @@ func TestCollectionPage_Count(t *testing.T) {
}
func TestToCollectionPage(t *testing.T) {
err := fmt.Errorf("unable to convert to collection page")
err := func(it Item) error { return fmt.Errorf("unable to convert %T to collection page", it) }
tests := map[string]struct {
it Item
want *CollectionPage
@ -152,17 +152,17 @@ func TestToCollectionPage(t *testing.T) {
"OrderedCollectionPage": {
it: new(OrderedCollectionPage),
want: new(CollectionPage),
wantErr: err,
wantErr: err(new(OrderedCollectionPage)),
},
"OrderedCollection": {
it: new(OrderedCollection),
want: new(CollectionPage),
wantErr: err,
wantErr: err(new(OrderedCollection)),
},
"Collection": {
it: new(Collection),
want: new(CollectionPage),
wantErr: err,
wantErr: err(new(Collection)),
},
}
for name, tt := range tests {

View file

@ -188,7 +188,7 @@ func TestCollection_ItemMatches(t *testing.T) {
}
func TestToCollection(t *testing.T) {
err := fmt.Errorf("unable to convert to collection")
err := func(it Item) error { return fmt.Errorf("unable to convert %T to collection", it) }
tests := map[string]struct {
it Item
want *Collection
@ -207,12 +207,12 @@ func TestToCollection(t *testing.T) {
"OrderedCollectionPage": {
it: new(OrderedCollectionPage),
want: new(Collection),
wantErr: err,
wantErr: err(new(OrderedCollectionPage)),
},
"OrderedCollection": {
it: new(OrderedCollection),
want: new(Collection),
wantErr: err,
wantErr: err(new(OrderedCollection)),
},
}
for name, tt := range tests {

View file

@ -3,7 +3,7 @@ package activitypub
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"reflect"
"time"
"unsafe"
@ -279,7 +279,7 @@ func ToIntransitiveActivity(it Item) (*IntransitiveActivity, error) {
}
}
}
return nil, errors.New("unable to convert to intransitive activity")
return nil, fmt.Errorf("unable to convert %T to intransitive activity", it)
}
// ArriveNew initializes an Arrive activity

View file

@ -502,7 +502,7 @@ func ToObject(it Item) (*Object, error) {
}
}
}
return nil, fmt.Errorf("unable to convert %q", it.GetType())
return nil, fmt.Errorf("unable to convert %T", it)
}
// Source is intended to convey some sort of source from which the content markup was derived,

View file

@ -3,7 +3,7 @@ package activitypub
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"reflect"
"time"
"unsafe"
@ -342,7 +342,7 @@ func ToOrderedCollection(it Item) (*OrderedCollection, error) {
}
}
}
return nil, errors.New("unable to convert to ordered collection")
return nil, fmt.Errorf("unable to convert %T to ordered collection", it)
}
func copyOrderedCollectionToPage(c *OrderedCollection, p *OrderedCollectionPage) error {

View file

@ -3,7 +3,7 @@ package activitypub
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"reflect"
"time"
@ -297,7 +297,7 @@ func ToOrderedCollectionPage(it Item) (*OrderedCollectionPage, error) {
}
}
}
return nil, errors.New("unable to convert to ordered collection page")
return nil, fmt.Errorf("unable to convert %T to ordered collection page", it)
}
// ItemsMatch

View file

@ -117,7 +117,7 @@ func TestOrderedCollectionPage_Contains(t *testing.T) {
}
func TestToOrderedCollectionPage(t *testing.T) {
err := fmt.Errorf("unable to convert to ordered collection page")
err := func(it Item) error { return fmt.Errorf("unable to convert %T to ordered collection page", it) }
tests := map[string]struct {
it Item
want *OrderedCollectionPage
@ -131,17 +131,17 @@ func TestToOrderedCollectionPage(t *testing.T) {
"OrderedCollection": {
it: new(OrderedCollection),
want: new(OrderedCollectionPage),
wantErr: err,
wantErr: err(new(OrderedCollection)),
},
"Collection": {
it: new(Collection),
want: new(OrderedCollectionPage),
wantErr: err,
wantErr: err(new(Collection)),
},
"CollectionPage": {
it: new(CollectionPage),
want: new(OrderedCollectionPage),
wantErr: err,
wantErr: err(new(CollectionPage)),
},
}
for name, tt := range tests {

View file

@ -214,7 +214,7 @@ func TestOnOrderedCollection(t *testing.T) {
}
func TestToOrderedCollection(t *testing.T) {
err := fmt.Errorf("unable to convert to ordered collection")
err := func(it Item) error { return fmt.Errorf("unable to convert %T to ordered collection", it) }
tests := map[string]struct {
it Item
want *OrderedCollection
@ -233,12 +233,12 @@ func TestToOrderedCollection(t *testing.T) {
"Collection": {
it: new(Collection),
want: new(OrderedCollection),
wantErr: err,
wantErr: err(new(Collection)),
},
"CollectionPage": {
it: new(CollectionPage),
want: new(OrderedCollection),
wantErr: err,
wantErr: err(new(CollectionPage)),
},
}
for name, tt := range tests {

View file

@ -275,7 +275,7 @@ func ToPlace(it Item) (*Place, error) {
}
}
}
return nil, fmt.Errorf("unable to convert %q", it.GetType())
return nil, fmt.Errorf("unable to convert %T to place", it)
}
type withPlaceFn func(*Place) error

View file

@ -243,7 +243,7 @@ func ToProfile(it Item) (*Profile, error) {
}
}
}
return nil, fmt.Errorf("unable to convert %q", it.GetType())
return nil, fmt.Errorf("unable to convert %T to profile", it)
}
type withProfileFn func(*Profile) error

View file

@ -3,7 +3,7 @@ package activitypub
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"reflect"
"time"
@ -252,5 +252,5 @@ func ToQuestion(it Item) (*Question, error) {
}
}
}
return nil, errors.New("unable to convert to Question activity")
return nil, fmt.Errorf("unable to convert %T to question", it)
}

View file

@ -258,7 +258,7 @@ func ToRelationship(it Item) (*Relationship, error) {
return i, nil
}
}
return nil, fmt.Errorf("unable to convert %q", it.GetType())
return nil, fmt.Errorf("unable to convert %T to relationship", it)
}
type withRelationshipFn func(*Relationship) error

View file

@ -249,7 +249,7 @@ func ToTombstone(it Item) (*Tombstone, error) {
}
}
}
return nil, fmt.Errorf("unable to convert %q", it.GetType())
return nil, fmt.Errorf("unable to convert %T to tombstone", it)
}
type withTombstoneFn func(*Tombstone) error