go-chi/chi · info

error

Error message

error

What it means

A literal errors.New("error") rendered by getArticle when the query string contains a non-empty 'error' parameter. It is a deliberate error-simulation hook so callers can observe how render.Respond serializes a plain error (through the overridden render.Respond in the rest example it becomes a 400 with {"status":"error"}, or in the default versions responder a body of {"error":"error"}). It carries no domain meaning and exists purely for demonstration.

Source

Thrown at _examples/versions/main.go:123

	if chi.URLParam(r, "articleID") != "1" {
		render.Respond(w, r, data.ErrNotFound)
		return
	}
	article := &data.Article{
		ID:                     1,
		Title:                  "Article #1",
		Data:                   []string{"one", "two", "three", "four"},
		CustomDataForAuthUsers: "secret data for auth'd users only",
	}

	// Simulate some context values:
	// 1. ?auth=true simulates authenticated session/user.
	// 2. ?error=true simulates random error.
	if r.URL.Query().Get("auth") != "" {
		r = r.WithContext(context.WithValue(r.Context(), "auth", true))
	}
	if r.URL.Query().Get("error") != "" {
		render.Respond(w, r, errors.New("error"))
		return
	}

	var payload render.Renderer

	apiVersion := r.Context().Value("api.version").(string)
	switch apiVersion {
	case "v1":
		payload = v1.NewArticleResponse(article)
	case "v2":
		payload = v2.NewArticleResponse(article)
	default:
		payload = v3.NewArticleResponse(article)
	}

	render.Render(w, r, payload)
}

View on GitHub (pinned to 8b258c7bb2)

Solutions

  1. Drop the ?error= query parameter from the request URL.
  2. Strip debug-only query params in your client before sending (e.g. delete params['error']).
  3. If you are forking the example, gate the simulated error behind an env var or remove the block for production.

Example fix

// before
GET /v2/articles/1?error=true
# -> 400 {"status":"error"} (or {"error":"error"})

// after
GET /v2/articles/1
# -> 200 with the v2 article payload
Defensive patterns

Strategy: validation

Validate before calling

// Strip the demo-only query param before sending.
u, _ := url.Parse(rawURL)
q := u.Query()
q.Del("error")
u.RawQuery = q.Encode()
finalURL := u.String()

Try / catch

// Treat the simulated error as a 4xx and stop; it is not transient.
if resp.StatusCode >= 400 && resp.StatusCode < 500 {
    body, _ := io.ReadAll(resp.Body)
    return fmt.Errorf("server returned %d: %s", resp.StatusCode, body)
}

Prevention

When it happens

Trigger: GET /v{1,2,3}/articles/1?error=true (or ?error=anything non-empty) in the versions example. See main.go:122-124.

Common situations: A developer copy-pasting a URL that still has ?error=true from a debugging session; an automated crawler that preserves all query params and re-triggers the fault; someone testing error handling and forgetting to remove the flag.

Related errors


AI-assisted analysis of go-chi/chi@8b258c7bb2 (2026-08-04). Data as JSON: /data/errors/b6ae5416b58e4b08.json. Report an issue: GitHub.