Skip to content

Commit

Permalink
Merge pull request #21 from zmkfirmware/feat/bettter-error-handling
Browse files Browse the repository at this point in the history
feat: Improved error handling.
  • Loading branch information
petejohanson authored Oct 2, 2024
2 parents c3411bb + 217536a commit 03ccb9c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
on:
pull:
push:
branches:
- main

name: checks

jobs:
checks:
strategy:
matrix:
command: [typecheck, test]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm run ${{ matrix.command }}
8 changes: 8 additions & 0 deletions src/transport/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class UserCancelledError extends Error {
constructor(m: string, opts: ErrorOptions) {
super(m, opts);

// Set the prototype explicitly.
Object.setPrototypeOf(this, UserCancelledError.prototype);
}
}
7 changes: 7 additions & 0 deletions src/transport/gatt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { RpcTransport } from './';
import { UserCancelledError } from './errors';

const SERVICE_UUID = '00000000-0196-6107-c967-c5cfb1c2482a';
const RPC_CHRC_UUID = '00000001-0196-6107-c967-c5cfb1c2482a';
Expand All @@ -7,6 +8,12 @@ export async function connect(): Promise<RpcTransport> {
let dev = await navigator.bluetooth.requestDevice({
filters: [{ services: [SERVICE_UUID] }],
optionalServices: [SERVICE_UUID],
}).catch((e) => {
if (e instanceof DOMException && e.name == "NotFoundError") {
throw new UserCancelledError("User cancelled the connection attempt", { cause: e});
} else {
throw e;
}
});

if (!dev.gatt) {
Expand Down
2 changes: 0 additions & 2 deletions src/transport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ export interface RpcTransport {
readable: ReadableStream<Uint8Array>;
writable: WritableStream<Uint8Array>;
}

export * as Gatt from './gatt';
9 changes: 8 additions & 1 deletion src/transport/serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ export async function connect(): Promise<RpcTransport> {
let abortController = new AbortController();
let port = await navigator.serial.requestPort({});

await port.open({ baudRate: 12500 });
await port.open({ baudRate: 12500 })
.catch((e) => {
if (e instanceof DOMException && e.name === "NetworkError") {
throw new Error("Failed to open the serial port. Check the permissions of the device and verify it is not in use by another process.", { cause: e });
} else {
throw e;
}
});

let info = port.getInfo();
let label =
Expand Down
8 changes: 4 additions & 4 deletions test/framing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ describe('framing', () => {
);

let reader = converted.getReader();
let chunks = [];
let chunks: Array<number> = [];

try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
if (done || !value) break;
chunks = chunks.concat(Array.from(value.values()));
}
} finally {
Expand All @@ -38,12 +38,12 @@ describe('framing', () => {
);

let reader = converted.getReader();
let chunks = [];
let chunks: Array<number> = [];

try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
if (done || !value) break;
chunks = chunks.concat(Array.from(value.values()));
}
} finally {
Expand Down

0 comments on commit 03ccb9c

Please sign in to comment.