oss.sarwagya.wtf

Error Handling

Every failure is a typed BluetoothError with a stable code.

BLE fails constantly: devices disappear, permissions get denied, connections drop. Every failure in Agnostic Web BLE is a BluetoothError — no parsing of browser error strings.

import { isBluetoothError } from "@0xsarwagya/agnostic-web-ble";
 
try {
  await device.connect();
} catch (error) {
  if (isBluetoothError(error) && error.code === "PERMISSION_DENIED") {
    // tell the user what they can do about it
  }
}

The error shape

{
  code: "CONNECTION_FAILED",   // stable, documented code
  message: "...",              // human-readable
  operation: "connect",        // which API call failed
  adapterId: "native-web-bluetooth",
  recoverable: true,           // worth retrying?
  cause: DOMException          // original runtime error, preserved
}

The original runtime error is never destroyed — it is attached as cause.

Error codes

CodeMeaning
UNSUPPORTEDThe runtime cannot perform this operation at all.
UNAVAILABLENo adapter is available in this runtime.
PERMISSION_DENIEDThe user or runtime refused access.
DEVICE_NOT_FOUNDNo device matched, or the chooser was dismissed.
CONNECTION_FAILEDThe GATT connection could not be established.
DISCONNECTEDThe operation ran against a dropped connection.
SERVICE_NOT_FOUNDService missing or not requested up front.
CHARACTERISTIC_NOT_FOUNDCharacteristic missing from the service.
READ_FAILED / WRITE_FAILEDThe GATT operation itself failed.
SUBSCRIPTION_FAILEDThe characteristic does not notify, or the runtime refused.
TIMEOUTThe operation was aborted or timed out.
ADAPTER_ERRORThe adapter failed internally.
UNKNOWNAnything an adapter could not classify.

Common failure

The most frequent real-world code is DEVICE_NOT_FOUND — usually the user closing the device chooser. Treat it as a normal outcome, not an exception worth alarming anyone about.