phibac developer docs

Errors and how to fix them

This page lists every error code your app can meet: every code the server sends, with its HTTP status, and the codes the SDK (the JavaScript library your page uses) makes itself. Find the code your app got, read what causes it, and apply the fix.

How errors reach your code

Every error the SDK gives your app is a PbError, an Error with four things you can read:

  • code: a short name such as consent_required. Decide what to do from the code, never from the message.
  • message: what went wrong, in a sentence.
  • hint: how to fix it.
  • extra: any other details the server sent, such as fields on schema_violation or retryAfter (in seconds) on rate_limited.

An error reaches your app in one of three ways, depending on the call:

  1. A rejected promise. connect(), a query's get(), requestAccess() and the three files calls reject with a PbError when they fail. So do create(), update() and delete(), for the problems the SDK finds before it queues the change: something that is not a plain object, a record that does not exist or was deleted (for a record that is not yet in the browser's copy of the data, the SDK asks the server first, so a server error can reject the call too), or a backend used after signOut() (unauthorized).
  2. onConflict. create(), update() and delete() do not wait for the server: the change goes into the write queue, the list of changes the server does not have yet, and the promise resolves at once. When the server later refuses a queued change for good, the SDK drops it, puts the record back as the server has it and calls the function you passed to backend.onConflict() with the error. See Offline writes and sync.
  3. status().problem. When a queued change fails for a reason that may pass, it stays queued and the SDK tries again later, waiting longer each time. When the reason is something the user must act on, backend.status().problem holds the error until it is solved. Live updates (the SDK telling your page when data changes) report their problems there too.

The code below handles all three. It expects a page with a Sign in button (#sign-in) and two paragraphs for messages (#message and #problem):

import { connect, PbError } from "@phibac/sdk";

interface Note {
  text: string;
  pinned?: boolean;
}

const signIn = document.querySelector<HTMLButtonElement>("#sign-in")!;
const message = document.querySelector<HTMLParagraphElement>("#message")!;
// The problem from status() gets its own line, so it never hides another message.
const problemLine = document.querySelector<HTMLParagraphElement>("#problem")!;
// One timer for the page, even when the user presses Sign in again.
let timer: ReturnType<typeof setInterval> | undefined;

// Turns any error into a sentence for the user.
function explain(e: unknown): string {
  if (!(e instanceof PbError)) return String(e);
  switch (e.code) {
    case "popup_blocked":
      return "Your browser blocked the sign-in window. Allow pop-ups for this site and try again.";
    case "sign_in_cancelled":
    case "access_denied":
      return "You are not signed in. Press Sign in to try again.";
    case "network":
      return "The server cannot be reached. Check your connection and try again.";
    case "unauthorized":
      return "Your sign-in has ended. Press Sign in again.";
    case "rate_limited":
      return `Too many requests. Try again in ${String(e.extra.retryAfter ?? "a few")} seconds.`;
    default:
      return `${e.code}: ${e.message} ${e.hint}`;
  }
}

signIn.addEventListener("click", async () => {
  try {
    const backend = await connect({ app: "app_...", baseUrl: "http://localhost:8787" });
    const notes = backend.collection<Note>("com.example.notes.note");

    // 2. A queued change the server refused for good.
    backend.onConflict(({ op, error }) => {
      message.textContent = `A change to a note was not saved (${op.kind}): ${explain(error)}`;
    });

    // 3. Something the user must act on. status() tells how things stand at the
    // moment you call it, so read it whenever the page shows its status: here,
    // every five seconds. The line is emptied once the problem is solved.
    clearInterval(timer);
    timer = setInterval(() => {
      const problem = backend.status().problem;
      problemLine.textContent = problem ? explain(problem) : "";
    }, 5000);

    // 1. A rejected promise.
    const pinned = await notes.where("pinned", "==", true).get();
    message.textContent = `${pinned.length} pinned notes.`;
  } catch (e) {
    message.textContent = explain(e);
  }
});

The developer tools report the same server codes. npx phibac deploy and the other CLI commands print the code, the message and the hint.

Every code at a glance

Code HTTP status Meaning
invalid_request 400 The request is malformed or breaks a rule.
unauthorized 401 The user is not signed in, or a token is missing, expired or revoked.
trial_ended 402 Sent only by hosted servers; this server does not send it.
forbidden 403 The app may not do this, whoever asks.
consent_required 403 The user has not allowed your app to read another app's collection.
app_suspended 403 The server's operator suspended the app.
not_found 404 The app, collection, version, record or file does not exist.
schema_incompatible 409 A schema change is breaking and needs a new major version.
record_deleted 410 The record was deleted.
payload_too_large 413 A record, file or request is over its size limit.
schema_violation 422 A record does not match its collection's schema.
version_unsupported 422 A major version that was never published.
rate_limited 429 Too many requests: wait, then try again.
internal 500 Something went wrong on the server.
storage_full 507 Sent only by hosted servers; this server does not send it.
network none (SDK) The server could not be reached.
invalid_response none (SDK) The server's answer was not what the SDK expected.
popup_blocked none (SDK) The browser did not open the sign-in or consent popup.
sign_in_cancelled none (SDK) The sign-in popup was closed before sign-in finished.
access_denied none (SDK) The user did not connect your app.
account_closed none (SDK) The user's account is closed or being deleted.

Server codes

invalid_request

What you see: a rejected promise from the call, an onConflict call for a queued change, or an error from npx phibac deploy.

Cause: the request is malformed or breaks a rule. The message says which. The usual ones:

  • A query filters or sorts on a field of a collection (a kind of data, such as notes) that is not indexed, the mark in the collection's schema (the description of its fields) that lets queries use a field: the message says the field "cannot be used to filter or order". Only indexed fields, createdAt and updatedAt can be used.
  • A query filter value does not fit the field's type, or an "in" list has more than 500 values.
  • A query on another app's collection does not say which major versions (the numbered versions of a schema) it reads: the message says "versions are required to query" it.
  • create() or update() got something that is not a plain object, where() got an unknown operator, or limit() got a number below 1. The SDK refuses these on the device, at once.
  • requestAccess() was called while a consent popup (where the user gives your app permission to read another app's collection) for another collection is still open, for a collection your app owns itself (an app needs no consent for its own collections), or for a sandbox. collection, which no other app can ever read.
  • When you publish: a missing description, a bad collection name, or a major version number that skips ahead or does not start at 1.
  • When you register origins (an origin is the scheme, host and port a page is served from, such as https://notes.example.com): there are more than 20, or one is not a bare origin (it has a path, for example), or it uses plain http:// on a host other than localhost, 127.0.0.1 or [::1].

Fix: change the call as the message and hint say. Sending it again unchanged fails again. For queries, mark the field indexed: true and publish again: see Query, sort and live updates. For another app's collection, open it with backend.collection(name, { versions: [1] }): see Read another app's data. For publishing problems, see Define your data and Change your schema safely.

unauthorized

What you see: a rejected promise from a query or a files call; queued changes stay queued and status().problem holds the error. After your app called signOut(), the collection and files calls on that backend throw or reject with it (status() and onConflict() still answer), and there are no queued changes left: signOut() deleted them. The CLI prints it when its developer token is refused.

Cause: in your app, the user is not signed in any more. The SDK renews the user's short-lived sign-in by itself and handles a wrong device clock by itself, so when you see this code, the stored sign-in has ended: it expired, the user disconnected your app in their account area, or your app called signOut() and then used the same backend again. The SDK then forgets the stored sign-in. For the CLI, the developer token is missing, mistyped, revoked or was made on another server.

Fix: in your app, show a Sign in button and call connect() again from its click. Once the user has signed in, the SDK sends the queued changes. See Sign users in. For the CLI, create a new developer token on the server you publish to (https://phibac.com/account/developer on this server) and export it as PB_TOKEN: see Put your app on the internet. On the local server the CLI uses the server's own key instead: see Develop on your own machine.

trial_ended

Cause: only hosted servers send this code; this server never does. Fix: nothing to do on this server.

forbidden

What you see: an onConflict call for a queued change; a rejected promise from files.upload or files.delete; or, when every request is refused, a rejected promise from a query too, while queued changes stay queued and status().problem holds the error. npx phibac deploy prints it too.

Cause: the app may not do this, and asking again will not change that:

  • Your app wrote to a collection another app owns. Access to another app's collection is read-only, even with the user's consent.
  • Your app uploaded a file to another app's collection, or deleted a file of a collection it does not own.
  • The page's origin is not registered for the app, and it is not http://localhost: the message says the app "may not be used from origin". The server then refuses every request, so the queue is kept and retried rather than dropped.
  • When you publish: another app already owns the collection name, or the app belongs to another account.

Fix: write only to your own collections and your own sandbox. collections (see Read another app's data). Register every origin your page runs from (see Sign users in). Publish under a collection name of a domain you control, with the token of the account that owns the app (see Define your data).

What you see: a rejected promise from a query's get() or from files.url. When your page subscribes to such a collection, the SDK leaves it out of live updates, keeps them for your other collections, and status().problem holds the error.

Cause: the collection belongs to another app, and the user has not allowed your app to read it, or has revoked that permission in their account area.

Fix: call requestAccess() on the collection from a click. It opens a consent popup and resolves true when the user allows; then query again. If it resolves false, show your app without that data, and do not ask again in a loop. See Read another app's data.

app_suspended

What you see: a rejected promise from any call that reaches the server; queued changes stay queued and status().problem holds the error; live updates stop. The sign-in popup says "This app has been suspended."

Cause: the operator of the server suspended your app. Every request it makes is refused.

Fix: nothing in your page can fix it. Show the user a message, and contact the server's operator. See Sign users in.

not_found

What you see: a rejected promise from update(), delete(), a query or a files call; an onConflict call for a queued change; or an error from npx phibac deploy, npx phibac generate or another CLI command.

Cause: what the call names does not exist:

  • No record with that rkey (the key that identifies a record within its collection) exists in the collection. The SDK checks this on the device before it queues an update or a delete.
  • The collection has no published schema yet: the message says the collection "has no published schema" or "is not published".
  • The file ID is wrong, the upload did not finish, or the file was deleted.
  • In the CLI, the app ID is wrong or belongs to another server.

Fix: check the rkey, the collection name and the file ID. Run npx phibac deploy before your app writes to a new collection, or use a sandbox. collection for data that needs no schema (see Define your data). Clear fields that still name a deleted file (see Upload and show files). In the CLI, use the app ID of the server you publish to (see Put your app on the internet).

schema_incompatible

What you see: npx phibac deploy shows the collection as breaking, with the reasons, such as field removed: color, and publishes nothing. Your app never gets this code.

Cause: you changed a schema in a breaking way within a major version: you removed or renamed a field, changed a field's type, made a field required or no longer required, added a required field, narrowed a range, lowered maxLength or removed an enum value.

Fix: undo the change, or publish it as the next major version by raising the collection's version by one in pb.config.ts. Additive changes, such as a new optional field, need no new major version. See Change your schema safely.

record_deleted

What you see: a rejected promise from update() when the browser's copy already shows the record as deleted, or an onConflict call when a queued change reaches the server after the record was deleted there.

Cause: the record was deleted, often on another device, for example while this one was offline. The SDK marks the record as deleted in the browser's copy.

Fix: create a new record instead; the SDK gives it a new rkey. localData in the onConflict call holds the data your app had, so you can save it again. The user can also restore the deleted record from their account area for 30 days; it then comes back under the same rkey, and your app receives it like any other change. See Read and write records and Collections and records.

payload_too_large

What you see: an onConflict call for a queued change that is too large, or a rejected promise from files.upload. The SDK sends the other queued changes as usual.

Cause: a record's data is over 256 KB, a file is over 100 MB, or a request is over its size limit. When you publish, a collection definition can also be too large.

Fix: keep records small. Store large content as a file and keep its ID in a fileRef field, or split the record. Make a file smaller before uploading it, for example by resizing an image. See Upload and show files.

schema_violation

What you see: an onConflict call for a queued change.

Cause: the record's data does not match the collection's schema. The error's extra.fields lists each field with the reason, such as required, longer than maxLength or unknown field. A common case after a new major version: a record written with the older version still has a field the new one removed.

Fix: fix the data and save it again; localData in the onConflict call holds what your app had. Set a removed field to null in the update. If the schema is what is wrong, publish the change you meant to make. The types npx phibac generate writes catch most of these mistakes before you run the code. See Read and write records and Change your schema safely.

version_unsupported

What you see: an onConflict call for a queued change, or a rejected promise from a query or requestAccess().

Cause: the write, the query or the versions list names a major version of the collection that was never published. The message lists the published ones.

Fix: use a published major version. Run npx phibac generate after every npx phibac deploy, so the SCHEMA_MAJORS your app writes with match what is published. See Change your schema safely.

rate_limited

What you see: a rejected promise whose extra.retryAfter says how many seconds to wait. The write queue waits at least that long and tries again; live updates try again by themselves, waiting longer each time.

Cause: too many requests. On every server, live updates are refused this way when the user's account holds too many tickets for live connections it has not used; the SDK opens a connection with each ticket it asks for, so this passes by itself. The account pages also refuse too many sign-ups or account recoveries from one network.

Fix: wait the number of seconds in extra.retryAfter, then try again, and never retry in a tight loop.

internal

What you see: a rejected promise, or queued changes that stay queued while the SDK tries again. The SDK also uses this code when the server answers with an error that has no details, and puts the HTTP status in extra.status.

Cause: something went wrong on the server, or something between your page and the server answered in its place.

Fix: try again later, waiting longer each time; the write queue does this by itself. If it keeps happening, check that baseUrl points at the server, and tell the server's operator.

storage_full

Cause: only hosted servers send this code; this server never does. Fix: nothing to do on this server.

SDK codes

The SDK makes these codes itself, so they have no HTTP status.

network

What you see: a rejected promise from connect() while it signs the user in, or from files.upload, files.delete, files.url or requestAccess(). A query's get() does not fail: offline, it answers from the browser's copy of the data instead. Queued changes stay queued.

Cause: the device is offline, the server cannot be reached at baseUrl, or it did not answer in time.

Fix: check the connection and baseUrl, then let the user try again. Uploads and file deletes are not queued, so your app must offer the retry. The write queue tries again by itself once the device is back online. See Upload and show files.

invalid_response

What you see: a rejected promise; queued changes stay queued and status().problem holds the error.

Cause: the server's answer is not what the SDK expected, for example a web page instead of JSON. Usually baseUrl points at a web page or a proxy rather than at the server.

Fix: check that baseUrl is the server's address, such as http://localhost:8787 while you develop.

What you see: a rejected promise from connect() or requestAccess().

Cause: the browser did not open the popup. Browsers allow a popup only as a direct result of a click or key press, so connect() without a stored sign-in, or requestAccess(), was called on page load, from a timer or after an await. Or the browser blocks popups for your site.

Fix: call connect() and requestAccess() first in a click handler, before any await. If it still happens, the user must allow popups for your site in the browser's settings. See Sign users in.

sign_in_cancelled

What you see: a rejected promise from connect().

Cause: the sign-in popup was closed before sign-in finished. The user closed it, or closed it after it showed an error page, such as one saying the app asked to sign in from an address it has not registered.

Fix: let the user press Sign in again. If the popup showed an error, fix what it said: see Sign users in.

access_denied

What you see: a rejected promise from connect().

Cause: the user pressed Cancel when asked to connect your app.

Fix: show your app without their data, and let them press Sign in again when they are ready. See Sign users in.

account_closed

What you see: status().problem holds the error and live updates stop for good.

Cause: the user's account is closed or being deleted.

Fix: nothing in your app can fix it. Tell the user that their account is closed. Live updates stay off for this backend.