glanceapp/glance · error

initializing primary request: %v

Error message

initializing primary request: %v

What it means

Returned by customAPIWidget.initialize when the widget's primary CustomAPIRequest.initialize() fails. That initializer validates the request definition — most notably the body-type must be json or string and the body must marshal — so this wraps config-level mistakes in the widget's top-level request block.

Source

Thrown at internal/glance/widget-custom-api.go:55

	httpRequest        *http.Request        `yaml:"-"`
}

type customAPIWidget struct {
	widgetBase        `yaml:",inline"`
	*CustomAPIRequest `yaml:",inline"`             // the primary request
	Subrequests       map[string]*CustomAPIRequest `yaml:"subrequests"`
	Options           customAPIOptions             `yaml:"options"`
	Template          string                       `yaml:"template"`
	Frameless         bool                         `yaml:"frameless"`
	compiledTemplate  *template.Template           `yaml:"-"`
	CompiledHTML      template.HTML                `yaml:"-"`
}

func (widget *customAPIWidget) initialize() error {
	widget.withTitle("Custom API").withCacheDuration(1 * time.Hour)

	if err := widget.CustomAPIRequest.initialize(); err != nil {
		return fmt.Errorf("initializing primary request: %v", err)
	}

	for key := range widget.Subrequests {
		if err := widget.Subrequests[key].initialize(); err != nil {
			return fmt.Errorf("initializing subrequest %q: %v", key, err)
		}
	}

	if widget.Template == "" {
		return errors.New("template is required")
	}

	compiledTemplate, err := template.New("").Funcs(customAPITemplateFuncs).Parse(widget.Template)
	if err != nil {
		return fmt.Errorf("parsing template: %w", err)
	}

	widget.compiledTemplate = compiledTemplate

View on GitHub (pinned to 91324e8de7)

Solutions

  1. Inspect the primary request block of the custom-api widget: url, body-type, body fields.
  2. Set body-type: json only when body is a YAML mapping/list; use string only for a plain string body.
  3. Re-check YAML indentation so subrequests: live under the widget, not merged into the primary request.
  4. Fix the underlying cause reported by the wrapped error — this message only adds the "primary request" context.

Example fix

# before
- type: custom-api
  url: https://example.com/api
  body-type: text

# after
- type: custom-api
  url: https://example.com/api
  body-type: json
Defensive patterns

Strategy: validation

Validate before calling

// sanity-check a custom-api primary request before glance loads it
func checkCustomAPIPrimary(r CustomAPIRequest) error {
    if r.URL == "" {
        return errors.New("custom-api: url is required")
    }
    if r.BodyType != "" && r.BodyType != "json" && r.BodyType != "string" {
        return fmt.Errorf("custom-api: body-type must be json or string, got %q", r.BodyType)
    }
    return nil
}

Try / catch

if err := widget.initialize(); err != nil {
    // initialization errors are config bugs: fail fast, show the widget as errored
    widget.withError(err) // or log and skip rendering this widget
}

Prevention

When it happens

Trigger: The main request block of a custom-api widget has an invalid body-type, a body that cannot be marshaled, or another failed validation performed by CustomAPIRequest.initialize().

Common situations: YAML indentation puts a subrequest-only field on the primary request; body-type typo like "JSON" or "text"; body containing a value json.Marshal rejects.

Related errors


AI-assisted analysis of glanceapp/glance@91324e8de7 (2026-08-15). Data as JSON: /api/errors/f7d51a2325a7b864. Report an issue: GitHub.