cayleygraph/cayley · error

token not valid

Error message

token not valid

What it means

NameOf expects the graph.Ref argument to be a *Token of Kind nodeKind so it can build a datastore key; anything else (a different Token kind, a non-Token ref) is rejected as invalid. It cannot resolve that reference to a node name.

Source

Thrown at graph/gaedatastore/quadstore.go:468

	return qs.newAllIterator(quadKind)
}

func (qs *QuadStore) ValueOf(s quad.Value) (graph.Ref, error) {
	id := hashOf(s)
	return &Token{Kind: nodeKind, Hash: id}, nil
}

func (qs *QuadStore) NameOf(val graph.Ref) (quad.Value, error) {
	if qs.context == nil {
		return nil, errors.New("context is nil, graph is not initialized")
	} else if v, ok := val.(refs.PreFetchedValue); ok {
		return v.NameOf(), nil
	}
	var key *datastore.Key
	if t, ok := val.(*Token); ok && t.Kind == nodeKind {
		key = qs.createKeyFromToken(t)
	} else {
		return nil, errors.New("token not valid")
	}

	// TODO (panamafrancis) implement a cache

	node := new(NodeEntry)
	err := datastore.Get(qs.context, key, node)
	if err != nil {
		return nil, err
	}
	return quad.Raw(node.Name), nil
}

func (qs *QuadStore) Quad(val graph.Ref) (quad.Quad, error) {
	if qs.context == nil {
		return quad.Quad{}, errors.New("context is nil, graph is not correctly initialized")
	}
	var key *datastore.Key
	if t, ok := val.(*Token); ok && t.Kind == quadKind {

View on GitHub (pinned to 81dcd7d73e)

Solutions

  1. Only pass Tokens obtained from NameFor/T-node paths (nodeKind) into NameOf.
  2. Use a type check before calling: t, ok := val.(*Token); ok && t.Kind == nodeKind.
  3. For quad tokens use Quad() instead of NameOf().
  4. Inspect ref provenance — convert hashes with NameFor rather than passing raw refs.

Example fix

// before
name, err := qs.NameOf(quadToken) // wrong kind
// after
if t, ok := ref.(*Token); ok && t.Kind == nodeKind {
    name, err = qs.NameOf(ref)
}
Defensive patterns

Strategy: type-guard

Validate before calling

if t, ok := ref.(*Token); !ok || t.Kind != nodeKind {
    return errors.New("NameOf requires a nodeKind Token")
}

Type guard

func isNodeToken(ref graph.Ref) bool {
    t, ok := ref.(*Token)
    return ok && t.Kind == nodeKind
}

Try / catch

val, err := qs.NameOf(ref)
if err != nil {
    if strings.Contains(err.Error(), "token not valid") {
        // ref is not a node token; use Quad() for quad tokens
    }
    return err
}

Prevention

When it happens

Trigger: Passing a *Token whose Kind is quadKind, a raw hash string ref, or another ref type into NameOf; mixing up tokens returned from quad lookups with node tokens.

Common situations: Application code caching Tokens and passing the wrong kind; iterating quad tokens into NameOf; API changes where refs became Token-typed but callers pass pre-conversion values.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of cayleygraph/cayley@81dcd7d73e (2026-09-06). Data as JSON: /api/errors/a426af53bde75619. Report an issue: GitHub.