Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improved error handling. #21

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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