gofr-dev/gofr · error

unexpected result type: expected []any

Error message

unexpected result type: expected []any

What it means

errUnexpectedResult is returned by extractRecord when the raw SurrealDB response has an unexpected dynamic type — gofr expects []any (a slice of results) but the SDK returned something else (e.g. a map, nil, or a scalar). It indicates a shape mismatch between what the SDK handed back and what gofr's extraction logic supports.

Source

Thrown at pkg/gofr/datasource/surrealdb/surrealdb.go:23

	"errors"
	"fmt"
	"math"
	"strings"
	"time"

	"github.com/surrealdb/surrealdb.go"
	"github.com/surrealdb/surrealdb.go/pkg/models"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/trace"
)

var (
	errNotConnected             = errors.New("not connected to database")
	errNoDatabaseInstance       = errors.New("failed to connect to SurrealDB: no valid database instance")
	errInvalidCredentialsConfig = errors.New("both username and password must be provided")
	errNoRecord                 = errors.New("no record found")
	errNoResult                 = errors.New("no result found in query response")
	errUnexpectedResult         = errors.New("unexpected result type: expected []any")
	errQueryError               = errors.New("query error")
)

const (
	schemeHTTP      = "http"
	schemeHTTPS     = "https"
	schemeWS        = "ws"
	schemeWSS       = "wss"
	schemeMemory    = "memory"
	schemeMem       = "mem"
	schemeSurrealkv = "surrealkv"
	statusOK        = "OK"

	defaultTimeout = 30 * time.Second
)

// Config represents the configuration required to connect to SurrealDB.
type Config struct {

View on GitHub (pinned to 187eb24962)

Solutions

  1. Align the surrealdb.go SDK version with the one gofr was built against (go.mod / go get surrealdb@compatible).
  2. Inspect the raw query response to see its actual shape and adjust the query to return an array.
  3. Check SurrealDB server version compatibility — newer servers may change result encoding.
  4. If persistent, report/patch the extraction logic for the response shape you receive.

Example fix

// before
require github.com/surrealdb/surrealdb.go v0.3.0 // mismatched response shape
// after
require github.com/surrealdb/surrealdb.go v0.5.1 // version gofr expects
Defensive patterns

Strategy: retry

Type guard

func isUnexpectedResult(err error) bool { return errors.Is(err, surrealdb.ErrUnexpectedResult) }

Try / catch

rec, err := client.Create(ctx, table, data)
if errors.Is(err, surrealdb.ErrUnexpectedResult) {
    // log raw response shape, check SDK/server versions, do not retry blindly
    return fmt.Errorf("response shape mismatch (check SDK version): %w", err)
}

Prevention

When it happens

Trigger: Calling Create/Update/Query paths that route through extractRecord where the SurrealDB SDK returns a non-slice payload — e.g. a single-object response, an error object encoded as a result, or an SDK version whose return type changed.

Common situations: Version drift between the surrealdb.go SDK and gofr's expected response shape, SurrealDB server returning an error payload in the result field, or direct queries whose result is a single object rather than an array.

Related errors


AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01). Data as JSON: /api/errors/c5480f9c8a8f9c3e. Report an issue: GitHub.