WriteLimitError writes a 402 Payment Required response with the LimitErrorBody payload. Centralized here so every Check* call site in HTTP handlers can convert the typed error uniformly: if err := enforcer.CheckAgentCreate(ctx, userID); err != nil { if limits.WriteLimitError(w, err) { return
(w http.ResponseWriter, err error)
| 40 | // written; false when the error was something else (caller must handle |
| 41 | // it themselves, typically as 5xx). |
| 42 | func WriteLimitError(w http.ResponseWriter, err error) bool { |
| 43 | le, ok := IsLimitExceeded(err) |
| 44 | if !ok { |
| 45 | return false |
| 46 | } |
| 47 | body := LimitErrorBody{ |
| 48 | Error: le.Error(), |
| 49 | Resource: le.Resource, |
| 50 | Limit: le.Limit, |
| 51 | Current: le.Current, |
| 52 | PlanCode: le.Limits.PlanCode, |
| 53 | UpgradeURL: le.Limits.UpgradeURL, |
| 54 | } |
| 55 | w.Header().Set("Content-Type", "application/json") |
| 56 | w.WriteHeader(http.StatusPaymentRequired) |
| 57 | _ = json.NewEncoder(w).Encode(body) |
| 58 | return true |
| 59 | } |
nothing calls this directly
no test coverage detected