Skip to content

Failures and the rejected list

A failure is either the network's or the server's. On the network's, the library serves the cache or keeps the write queued; on the server's, it hands you supabase-kt's own exception, unchanged. A queued write the server refuses later, on replay, is thrown at no one: it goes to the rejected list, which your app shows; one whose row moved on the server meanwhile is a conflict (Conflicts). The samples turn every failure a call can throw into their own words in one place, a branch per kind:

From the samples: LibraryFailures.kt · SampleFailure.swift

/**
 * Every failure a library call can throw, in the app's terms: one branch for each kind, each with a
 * sentence of its own on screen. The library's own types come first, then supabase-kt's, which the
 * library passes through unchanged.
 */
internal fun Throwable.toAppFailure(call: LibraryCall): Throwable =
    when {
        this is CancellationException -> this // never a failure
        // Simulate offline is on, and the request needed the server.
        this is NetworkDisabledException -> NetworkSwitchedOffException(this)
        // A cache-only read found nothing saved for its query.
        this is CacheMissException -> NotSavedOnDeviceException(this)
        // A write the queue cannot hold found no connection; nothing was queued.
        this is MutationNotQueueableException ->
            WriteNeedsConnectionException(message.orEmpty(), this)
        // Why a replay stopped, writes still queued: the status and code, never the message.
        this is SyncFailedException ->
            SyncStoppedException(httpStatus, code, isNetworkOrConnectivityFailure(), this)
        // A kind of failure a later release of the library adds.
        this is OfflineException -> UnknownLibraryFailureException(this)
        // The client is closed: every call after close() throws it.
        this is OfflineClientClosedException -> ClientUnavailableException(this)
        // The session's token expired and could not be refreshed, here or on the server.
        this is TokenExpiredException || httpStatusOrNull() == HTTP_UNAUTHORIZED ->
            SessionExpiredException(this)
        // PostgREST refused it: a write's refusal names the task, anything else its status.
        this is PostgrestRestException -> serverRefusal(call)
        // Storage refused it, or has no such file.
        this is RestException -> storageRefusal(call)
        // No connection, or no answer in time: the network, not the server.
        isNetworkOrConnectivityFailure() -> NetworkUnavailableException(this)
        // A row that does not decode into the app's type, or a value that does not encode.
        this is SerializationException || this is SupabaseEncodingException ->
            RowFormatException(this)
        // An empty pattern list, a quoted `in` value, or a retry of a write that cannot be
        // retried.
        this is IllegalArgumentException -> InvalidRequestException(message.orEmpty(), this)
        else -> this // unexpected: reported as it is
    }
/// A write's failure: the server's refusal of a write sent at once, named by its task. Any
/// other failure stays the library's, for ``sentence(for:)``.
static func write(_ error: any Error, taskTitle: String?) -> any Error {
    guard let offline = error as? OfflineError, offline.code == .server, let serverError = offline.serverError else {
        return error
    }
    return SampleFailure.writeRefused(taskTitle: taskTitle, serverError: serverError)
}

/// The app's sentence for each of the library's failures, by its code.
static func sentence(for error: OfflineError) -> String {
    switch error.code {
    case .networkDisabled:
        return "Simulate offline is on, and that needs the server. Switch it off on the Sync tab."
    case .connectivity:
        return "That needs the network, and the server can’t be reached. Try again when you’re online."
    case .cacheMiss:
        return "Nothing for that is saved on this device yet. Open it once while you’re online."
    case .mutationNotQueueable:
        return "That change can’t wait in the queue, and the server can’t be reached, so nothing was changed. (\(error.message))"
    case .syncFailed:
        return "The last sync stopped early\(Wording.serverAnswer(status: error.httpStatus, code: error.serverError?.code)). The rest waits, and is sent on the next try."
    case .server:
        return error.serverError.map(Wording.refusalReason) ?? "The server refused it."
    case .clientClosed:
        return "The app’s offline data is closed. Restart the app."
    case .accessToken:
        return "Your sign-in couldn’t be read, so nothing was sent. It’s tried again with the next request."
    case .invalidRequest:
        return "The app asked for something that can’t be sent as written. Nothing was sent."
    case .decoding:
        return "The server’s answer couldn’t be read as the app expects."
    case .encoding:
        return "The change couldn’t be written out to send. Nothing was saved."
    case .configuration:
        return "The app’s offline storage couldn’t be opened. Restart the app."
    case .unexpected:
        return "Something went wrong. Try again."
    default:
        // A code a later release of the library adds.
        return "Something went wrong (\(error.code)). Try again."
    }
}

How a failure is classified

  • isNetworkOrConnectivityFailure(), which the library uses itself, is true for a timeout or an I/O failure, here or in a cause, such as supabase-kt's HttpRequestException; never for a cancellation, and never for anything carrying an HTTP status. A read falls back to the cache on such a failure only, and a reactive read reports a failing refresh in its snapshot instead of throwing (Refresh lifecycle).
  • httpStatusOrNull() is the HTTP status the server answered with, read from supabase-kt's RestException (PostgREST's and Storage's errors), a Ktor ResponseException or a SyncFailedException, here or in a cause.

The library's own failures subclass OfflineException, all but the closed client's (below). The hierarchy is open, so match the ones you handle and keep a branch for the rest; everything else is supabase-kt's or the server's (PostgrestRestException, RestException, HttpRequestException, TokenExpiredException, …):

Exception When
CacheMissException A one-shot CacheOnly read found nothing it may serve, or any CacheOnly read asked for a shape the cache never holds. Nothing was sent. A live CacheOnly read of a query that is not cached emits null rows, or only the queued writes it admits, instead
MutationNotQueueableException A write the queue cannot hold could not be sent for want of a connection. Its cause is that failure, so it counts as a connectivity failure
NetworkDisabledException A request that must reach the server was made after disableNetwork(). It counts as a connectivity failure
SyncFailedException Why a sync pass stopped with writes still queued, as syncStatus.value.lastSyncError says. It is never thrown; it carries httpStatus and code only

Every call on a closed client throws OfflineClientClosedException, and so does a call the closing stopped. It is an IllegalStateException, not an OfflineException, so a catch (e: IllegalStateException) catches it, and its message is "This offline client is closed". Match the type, not the message: another IllegalStateException can say nothing about the client.

Writes

Failure When
supabase-kt's PostgrestRestException The write was sent at once and the server refused it, with any answer but those that keep it queued (below). It does not stay queued
MutationNotQueueableException A write the queue cannot hold (a dry run, a filter it cannot record, another schema, an unknown user) could not be sent for want of a connection. Nothing was queued
The connectivity failure itself An OnlineOnly write could not be sent
NetworkDisabledException An OnlineOnly write was made while network access is off
IllegalArgumentException An UPDATE or DELETE named an empty pattern list, or an in element or group value holding " or \ the server would read with its escapes; nothing was sent or queued

A QueueIfOffline write that may not have run, or may pass when sent again, is not thrown: no connection or no answer, a 408, 429, 502, 503, 504, 520, 522 or 524, or a 401 for a token PostgREST holds expired or cannot read (PGRST301, PGRST303). It returns Queued, and a later pass sends it (What a write returns); a replay keeps a write queued on each of these answers too. Another 5xx, such as a 500 from the database, is thrown. When you show a refusal, show PostgREST's own error and code, or your own words keyed on the code: the exception's message carries the request's URL with its filter values, so keep it out of the screen and out of crash reports (Security and privacy).

Replay failures

Which replay failures stop the pass and which move a write to the rejected list is on What happens to each write. A write the server keeps failing with a 5xx holds every later write of its user behind it: when it will never go through, discardPendingMutation(id) removes it, unsent, and the writes behind it go.

The rejected list

It is kept on the device across restarts until each entry is dismissed or retried. rejectedMutations() follows it as it changes, and rejectedMutationList() reads it once, as the flow's first list. Each entry is a RejectedMutation: the table, the operation, the rows' keys, the body, the server's answer as a ServerError (status, code, message, details, hint), and whether it can be retried.

From the samples: OfflineTaskRepository.kt · LibraryMappings.kt · TaskRepository.swift · QueueWording.swift

/** The refused writes, newest first (the library lists the oldest first), until dismissed. */
override fun rejectedMutations(): Flow<List<RejectedTaskMutation>> =
    offline.rejectedMutations().map { list ->
        list.asReversed().map { it.toRejectedTaskMutation() }
    }


/** A refused write as the Sync tab shows it: when it was made and refused, and why. */
internal fun RejectedMutation.toRejectedTaskMutation(): RejectedTaskMutation =
    RejectedTaskMutation(
        id = id,
        table = table,
        operation = operation.toTaskMutationOperation(),
        rowKeys = rowKeys.mapNotNull { it.idOrNull() },
        taskTitle = body?.titleOrNull(),
        // Null when the library refused it itself: it could no longer be replayed as made.
        serverError = serverError?.toTaskServerError(),
        message = message,
        rejectedAt = rejectedAt,
        createdAt = createdAt,
        isRetryable = isRetryable, // false when its body was too long to keep
    )

/** The server's answer to a refused write, kept as data so it survives a restart. */
internal fun ServerError.toTaskServerError(): TaskServerError =
    TaskServerError(status = status, code = code, message = message, details = details, hint = hint)


/** Dismiss: the refused write is forgotten for good. */
override suspend fun dismissRejectedMutation(id: Long) {
    offline.dismissRejectedMutation(id)
}

/** Retry: queued again at the back, and sent by a pass; a second refusal returns here. */
override suspend fun retryRejectedMutation(id: Long) {
    try {
        offline.retryRejectedMutation(id)
    } catch (notRetryable: IllegalArgumentException) {
        throw notRetryable.toAppFailure(LibraryCall.Sync) // its body was too long to keep
    }
}
/// The writes the server refused on replay, oldest first, until each is dismissed or retried.
func rejectedMutations() -> RejectedMutationSequence {
    client.rejectedMutations()
}

/// The refused writes as they are now, read once: the ones the app found at launch.
func rejectedMutationsNow() async throws -> [RejectedMutation] {
    try await client.rejectedMutations().first()
}

/// What a refused write did, when it was made and refused, and the rows it was for: "Added
/// “Buy milk” · made 10:41 · refused 10:42 · row 3f2a…".
static func refusedChange(_ rejection: RejectedMutation) -> String {
    let what = change(rejection.operation, table: rejection.table, body: rejection.body)
    let made = rejection.createdAt.formatted(date: .omitted, time: .shortened)
    let refused = rejection.rejectedAt.formatted(date: .omitted, time: .shortened)
    return "\(what) · made \(made) · refused \(refused) · \(rowKeys(rejection.rowKeys))"
}

/// Forgets a refused write.
func dismissRejectedMutation(id: Int64) async throws {
    try await client.dismissRejectedMutation(id: id)
}

/// Queues a refused write again, at the back of the queue; a second refusal comes back on the
/// rejected list, never as an error.
func retryRejectedMutation(id: Int64) async throws {
    try await client.retryRejectedMutation(id: id)
}

Retry a write once its cause is fixed (a missing parent row, a policy): it rejoins the queue at the end under a new id. A write that was guarded by a version column keeps its base, so its retry is checked against the server's row as its first send was: a row whose columns the write sets moved meanwhile is a conflict for your resolver, and one moved on other columns only is resent without asking (Conflicts); neither is an overwrite. Postgres puts the offending values in details (a duplicate key's value, for one), so show your own words rather than the raw fields.

Its bounds. The newest 100 entries of every user together, within 512 K characters. A body longer than 16 K characters is not kept, so that entry cannot be retried (isRetryable is false); the server's message, details and hint are kept to 4 K characters each. A guarded write's base row (the columns it sets, as the device saw them) longer than 16 K characters is not kept either, logged at WARNING: the retry is guarded by its version alone, so any move of the row is a conflict for your resolver, with a base that holds only the version. When the list is full the oldest entries go, and their writes are lost: nothing reports the loss yet.

Conflicts

A table whose version column you name (Configuration) has its queued updates and deletes of one cached row checked when they replay: each is sent only while the server's row still has the version the device saw, and a row that moved meanwhile is a conflict your resolver settles. Conflicts and merging walks through it, with a running example.

The replay finds An UPDATE A DELETE
The row at the version the device saw Applied; the server's row is cached Applied
No row Gone: the resolver is asked with server = null; by default the write is dropped (conflictsKeptServerCount) Already gone: synced (alreadyOnServerCount)
A row the rest of the write's filter no longer reaches Synced, as the unguarded replay's empty answer was The same
The row at that version, still meeting the filter, unchanged A policy denies it: the rejected list The same
The row moved, already holding the write's values An earlier send landed: synced (alreadyOnServerCount) —
The row moved on other columns only Resent at the new version, without asking Any move: a conflict, by default KeepLocal (deleted)
The row moved, otherwise A conflict: the resolver, by default KeepLocal (resent at the new version) The same as any move
No connection The pass stops; the write stays queued The same

A resolver that answers Hold, throws or takes 30 seconds parks the write on conflictedMutations(), for the app to resolve or dismiss; so does a third send that still finds the row moved. While the write waits there, the cached row shows the server's values for the columns the replay read, when it read one, under the version the device saw.

Everything else keeps the last write wins:

  • A replayed UPSERT, any write to a table without a version column, a write sent at once, and an UPDATE or DELETE that pins no single cached row send what they were given; if a first send reached the server but the answer was lost, the replay overwrites what another writer changed in between, and an upsert recreates a row deleted elsewhere.
  • A value several devices change (a counter, a running total) belongs in a server-side computation: a queued write replays exactly the value it set, so write the facts that feed it.
  • A policy change under row-level security can turn writes already queued into refusals.
  • A parent row deleted elsewhere makes a queued child's insert fail on its foreign key (23503), and it lands on the rejected list. Your own child queued after its parent is sent after it.
  • The same id twice. A genuine duplicate id is refused (23505) and lands on the rejected list (Upserts and ids).
  • On iOS, a request that fails as the network path drops can read as never sent, and its replay may be refused as a duplicate of itself: a false rejection, never a lost write.

A missing Storage object

Storage answers a request for a missing object with HTTP 400 and a 404 in its body. supabase-kt raises every Storage failure as a StorageRestException that carries the body's status, so httpStatusOrNull() reads 404: test the status, not the type. A Storage refusal on the rejected list carries the same status, and its code is Storage's own error code, such as NoSuchKey. The offline bucket drops its cached copy when the server says so (Storage caching).