micro/go-micro · error

ErrNotFound

ErrNotFound

Error message

token not found

What it means

ErrNotFound (message "token not found") is defined in auth/jwt/token and is returned when a token cannot be found, e.g. looking up a token by service name in the registry or reading a token from storage/metadata where none exists. It is a lookup-miss sentinel, not a decode or signature error.

Source

Thrown at auth/jwt/token/token.go:12

package token

import (
	"errors"
	"time"

	"go-micro.dev/v6/auth"
)

var (
	// ErrNotFound is returned when a token cannot be found.
	ErrNotFound = errors.New("token not found")
	// ErrEncodingToken is returned when the service encounters an error during encoding.
	ErrEncodingToken = errors.New("error encoding the token")
	// ErrInvalidToken is returned when the token provided is not valid.
	ErrInvalidToken = errors.New("invalid token provided")
)

// Provider generates and inspects tokens.
type Provider interface {
	Generate(account *auth.Account, opts ...GenerateOption) (*Token, error)
	Inspect(token string) (*auth.Account, error)
	String() string
}

type Token struct {
	// The actual token
	Token string `json:"token"`
	// Time of token creation
	Created time.Time `json:"created"`

View on GitHub (pinned to 24529f1404)

Solutions

  1. Ensure the JWT auth service is running and registered under the expected name before loading its token
  2. Verify the registry configuration (address, namespace) matches where tokens were written
  3. Call Generate/Refresh to create a token before attempting to Load/Read it
  4. If reading from metadata, confirm the Authorization/metadata key is present on the request or connection

Example fix

// before: Load fails with ErrNotFound because nothing was generated
svc, err := provider.Load("missing-service")
// after: generate the token first, then load
if _, err := provider.Generate(account); err != nil { return err }
tok, err := provider.Load("missing-service")
Defensive patterns

Strategy: validation

Validate before calling

// confirm the auth service/token record exists before Load
services, err := registry.ListServices()
if err != nil { return err }
found := false
for _, s := range services {
    if s.Name == "go.micro.auth" { found = true; break }
}
if !found { return errors.New("auth service not registered; cannot load token") }

Type guard

func isTokenNotFound(err error) bool {
    return errors.Is(err, token.ErrNotFound)
}

Try / catch

tok, err := provider.Load(serviceName)
if err != nil {
    if errors.Is(err, token.ErrNotFound) {
        // fall back to generating a fresh token
        tok, err = provider.Generate(account)
    }
    if err != nil { return err }
}

Prevention

When it happens

Trigger: Load/Read is called for a token record that does not exist; next or serviceWithName cannot resolve the expected service holding the token in the registry; TokenFromMetadata receives metadata with no token key.

Common situations: JWT auth service not running or not registered so the token record was never written; pointing a client at the wrong namespace/environment; calling Load before Generate/Refresh ever persisted a token; registry misconfiguration (wrong registry address) hiding existing records.

Related errors


AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01). Data as JSON: /api/errors/190cd198f9cf00df. Report an issue: GitHub.