hasura/graphql-engine · error

jsonparser: %w

Error message

jsonparser: %w

What it means

This error is returned by metadatautil.GetSourceKind when the underlying jsonparser library fails while walking the 'sources' array of the exported Hasura metadata JSON. It wraps the jsonparser error verbatim, so the root cause (malformed JSON, wrong type at a key, or key not found) is visible only in the wrapped message. It indicates the metadata export the CLI fed in could not be parsed at the expected shape.

Source

Thrown at cli/internal/metadatautil/sources.go:48

	var kind *string

	_, err = jsonparser.ArrayEach(
		metadata,
		func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
			var v string

			v, _ = jsonparser.GetString(value, "name")
			if v == sourceName {
				k, _ := jsonparser.GetString(value, "kind")
				if len(k) > 0 {
					kind = &k
				}
			}
		},
		"sources",
	)
	if err != nil {
		return nil, internalerrors.E(op, fmt.Errorf("jsonparser: %w", err))
	}

	if kind != nil {
		return (*hasura.SourceKind)(kind), nil
	}

	return nil, nil
}

func GetSources(exportMetadata func() (io.Reader, error)) ([]string, error) {
	var op internalerrors.Op = "metadatautil.GetSources"

	metadataReader, err := exportMetadata()
	if err != nil {
		return nil, internalerrors.E(op, err)
	}

	metadata, err := io.ReadAll(metadataReader)

View on GitHub (pinned to 724551b9ae)

Solutions

  1. Inspect the full wrapped message: if it says 'Key path not found' the metadata has no sources; if 'cannot unmarshal' the JSON type at sources/kind is wrong
  2. Print/export the metadata (hasura metadata export) and validate it with jq . to confirm it is well-formed JSON
  3. Verify the Hasura server version matches the CLI version (metadata formats differ between v1/v2/v3)
  4. If the reader comes from an HTTP call, check the server actually returned JSON (status code, content-type)

Example fix

// before
kind, err := metadatautil.GetSourceKind(exportMetadata)
if err != nil { return err }

// after
kind, err := metadatautil.GetSourceKind(exportMetadata)
if err != nil {
    // surface the wrapped jsonparser cause and the raw metadata for debugging
    return fmt.Errorf("getting source kind (is metadata valid JSON?): %w", err)
}
Defensive patterns

Strategy: validation

Validate before calling

r, err := exportMetadata()
if err != nil { return err }
raw, _ := io.ReadAll(r)
if !json.Valid(raw) { return errors.New("metadata is not valid JSON") }

Type guard

func isParsableMetadata(r io.Reader) bool {
    b, err := io.ReadAll(r)
    return err == nil && json.Valid(b)
}

Try / catch

if err != nil {
    return fmt.Errorf("getting source kind: %w", err) // inspect wrapped jsonparser cause
}

Prevention

When it happens

Trigger: Calling GetSourceKind(exportMetadata) where exportMetadata returns a reader whose JSON is invalid, or where the 'sources' key exists but an element's 'kind' field is not a string (jsonparser.ArrayEach / GetString each error). Typical with corrupt metadata files or an endpoint returning HTML/error bodies instead of JSON.

Common situations: Metadata exported from an incompatible Hasura version, a truncated metadata.json, or an API endpoint behind a proxy returning a login page. Also occurs when migrating projects whose metadata structure changed between CLI versions.

Related errors


AI-assisted analysis of hasura/graphql-engine@724551b9ae (2026-08-28). Data as JSON: /api/errors/c2cacd8953bbaae7. Report an issue: GitHub.