slimtoolkit/slim · error

error encoding updated package data

Error message

error encoding updated package data

What it means

nodePackageJSONVerUpdater rewrites a package.json by re-encoding the parsed metadata with a json.Encoder into a buffer. If encoding the updated package info fails, it returns this error. Note the underlying error is discarded, so the message does not include the cause. In practice json.Encoder.Encode on an in-memory buffer rarely fails unless the value is invalid (e.g. contains an unsupported value such as a channel, func, or cyclic structure).

Source

Thrown at pkg/app/sensor/artifact/artifact.go:235

	}

	version, ok := info["version"].(string)
	if !ok {
		log.Tracef("nodePackageJSONVerUpdater - no version field, return as-is")
		return data, nil
	}

	version = fmt.Sprintf("1%s", version)
	log.Tracef("nodePackageJSONVerUpdater(%s) - version='%v'->'%v')\n", target, info["version"], version)
	info["version"] = version

	var b bytes.Buffer
	enc := json.NewEncoder(&b)
	enc.SetEscapeHTML(false)
	enc.SetIndent("  ", "  ")
	err = enc.Encode(info)
	if err != nil {
		return nil, fmt.Errorf("error encoding updated package data")
	}

	return b.Bytes(), nil
}

var fileTypeCmd string

func init() {
	findFileTypeCmd()
}

func findFileTypeCmd() {
	fileTypeCmd, err := exec.LookPath(fileTypeCmdName)
	if err != nil {
		log.Debugf("findFileTypeCmd - cmd not found: %v", err)
		return
	}

View on GitHub (pinned to 81940d17fa)

Solutions

  1. Include the underlying error in the message (%w / %v) to diagnose the actual cause.
  2. Validate that all fields added to info are JSON-serializable before encoding.
  3. Marshal to a staging variable first and inspect it if the failure is reproducible.
  4. Ensure any custom MarshalJSON implementations on the package-info types do not return errors.

Example fix

// before
if err != nil {
    return nil, fmt.Errorf("error encoding updated package data")
}
// after
if err != nil {
    return nil, fmt.Errorf("error encoding updated package data: %w", err)
}
Defensive patterns

Strategy: try-catch

Type guard

func isJSONSerializable(v any) bool {
    _, err := json.Marshal(v)
    return err == nil
}

Try / catch

out, err := nodePackageJSONVerUpdater(pkgJSON, ver)
if err != nil {
    if strings.Contains(err.Error(), "error encoding updated package data") {
        // re-parse and re-marshal to sanitize non-JSON-safe fields
        var sanitized map[string]any
        _ = json.Unmarshal(pkgJSON, &sanitized)
        _ = json.Marshal(sanitized) // surfaces the real marshal error
        return err
    }
    return err
}

Prevention

When it happens

Trigger: Calling nodePackageJSONVerUpdater on package metadata that json cannot marshal — e.g. info was mutated to hold an unsupported type, or a custom marshaler on a field returns an error.

Common situations: Programmatic edits injecting non-JSON-safe values into the parsed package map; a custom MarshalJSON hook erroring; extremely rare buffer/encoder faults.

Related errors


AI-assisted analysis of slimtoolkit/slim@81940d17fa (2026-08-31). Data as JSON: /api/errors/e1735969057f488d. Report an issue: GitHub.