Docs/Recipes

Handle errors and limits

Every error has a code and a message written for your users.

errors.js
try {
  const reply = await charge.ai.chat(text)
  out.textContent = reply.text
} catch (e) {
  if (e.code === 'cancelled') return          // the user closed a sheet
  out.textContent = e.message                  // already written for the user
}
  • cancelled: the user closed the sign-in or payment sheet. Do nothing.
  • daily_cap: your app reached the daily limit this user set. ChargeMyAI already offered them their account.
  • paused: the user paused spending in all apps.
  • popup_blocked: a preview frame blocked the sign-in window. Elsewhere, when a window can't open, charge.js signs in on the same page instead.
  • not_web_page: the page was opened as a file (file://). Serve the folder from a local web server, for example python3 -m http.server 8000, and open the address it prints.
  • abroad_not_allowed: the model runs outside India and the user didn't allow that. ChargeMyAI already asked them; they can allow it any time in their account.
  • device_unavailable: you asked for mode: 'device' and the device can't answer.
  • upstream_unavailable: no model answered. The user was not charged.
  • bad_file, too_large, files_unsupported: a file isn't PNG, JPEG, WebP or PDF, the files pass about 3 MB, or no model here reads files.
  • invalid_json: with a schema, the reply didn't match it twice. Both tries were charged.
  • invalid_schema: the schema you passed isn't valid JSON Schema. Fix it in your code.

Read next

Still stuck? Talk to us and we'll help.