diff --git a/lib/bindings.test.ts b/lib/bindings.test.ts index 83745b2e..1e987cfb 100644 --- a/lib/bindings.test.ts +++ b/lib/bindings.test.ts @@ -3,6 +3,7 @@ import { makeTestFeature } from '../test/makeTestFeature' import { BindingInterface, OpenOptions, PortInfo, SetOptions } from '@serialport/bindings-interface' import { autoDetect } from './index' import MockBinding from '@serialport/binding-mock' +import { BindingsError } from './errors' // All bindings are required to work with an "echo" firmware const TEST_PORT = process.env.TEST_PORT @@ -249,7 +250,7 @@ function testBinding(bindingName: string, Binding: BindingInterface, testPort?: const port = await Binding.open(options) const readError = shouldReject(port.read(Buffer.alloc(100), 0, 100)) await port.close() - const err = await readError + const err: BindingsError = await readError assert.isTrue(err.canceled) }) }) diff --git a/lib/errors.ts b/lib/errors.ts index 36a23130..14d605c8 100644 --- a/lib/errors.ts +++ b/lib/errors.ts @@ -1,7 +1,9 @@ -export class CanceledError extends Error { - canceled: true - constructor(message: string) { +import {BindingsErrorInterface} from '@serialport/bindings-interface' + +export class BindingsError extends Error implements BindingsErrorInterface { + canceled: boolean + constructor(message: string, { canceled = false } = {}) { super(message) - this.canceled = true + this.canceled = canceled } } diff --git a/lib/poller.ts b/lib/poller.ts index 6bd76c8b..fcb0d520 100644 --- a/lib/poller.ts +++ b/lib/poller.ts @@ -2,7 +2,7 @@ import debug from 'debug' import { EventEmitter } from 'events' import { join } from 'path' import nodeGypBuild from 'node-gyp-build' -import { CanceledError } from './errors' +import { BindingsError } from './errors' const { Poller: PollerBindings } = nodeGypBuild(join(__dirname, '../')) as any const logger = debug('serialport/bindings/poller') @@ -99,7 +99,7 @@ export class Poller extends EventEmitter { } emitCanceled(): void { - const err = new CanceledError('Canceled') + const err = new BindingsError('Canceled', { canceled: true }) this.emit('readable', err) this.emit('writable', err) this.emit('disconnect', err) diff --git a/lib/unix-read.test.ts b/lib/unix-read.test.ts index 5286c5a3..9c7a4e12 100644 --- a/lib/unix-read.test.ts +++ b/lib/unix-read.test.ts @@ -1,3 +1,4 @@ +import { BindingsError } from '.' import { assert, shouldReject } from '../test/assert' import { LinuxPortBinding } from './linux' import { unixRead } from './unix-read' @@ -97,7 +98,7 @@ describe('unixRead', () => { (mock as any).isOpen = false makeFsReadError('EAGAIN')() } - const err = await shouldReject(unixRead({ binding: mock, buffer: readBuffer, offset: 0, length: 8, fsReadAsync })) + const err: BindingsError = await shouldReject(unixRead({ binding: mock, buffer: readBuffer, offset: 0, length: 8, fsReadAsync })) assert.isTrue(err.canceled) }) it('rejects a disconnected error when fsread errors a disconnect error', async () => { diff --git a/lib/unix-read.ts b/lib/unix-read.ts index cc6affe9..8e15beca 100644 --- a/lib/unix-read.ts +++ b/lib/unix-read.ts @@ -1,6 +1,6 @@ import { promisify } from 'util' import { read as fsRead } from 'fs' -import { CanceledError } from './errors' +import { BindingsError } from './errors' import debugFactory from 'debug' import { LinuxPortBinding } from './linux' import { DarwinPortBinding } from './darwin' @@ -34,7 +34,7 @@ export const unixRead = async ({ }: UnixReadOptions): Promise<{ buffer: Buffer; bytesRead: number }> => { logger('Starting read') if (!binding.isOpen || !binding.fd) { - throw new CanceledError('Port is not open') + throw new BindingsError('Port is not open', { canceled: true }) } try { @@ -48,7 +48,7 @@ export const unixRead = async ({ logger('read error', err) if (err.code === 'EAGAIN' || err.code === 'EWOULDBLOCK' || err.code === 'EINTR') { if (!binding.isOpen) { - throw new CanceledError('Port is not open') + throw new BindingsError('Port is not open', { canceled: true }) } logger('waiting for readable because of code:', err.code) await readable(binding) diff --git a/lib/win32.ts b/lib/win32.ts index ebbeb54d..7cedd8aa 100644 --- a/lib/win32.ts +++ b/lib/win32.ts @@ -1,5 +1,5 @@ import debugFactory from 'debug' -import { BindingPortInterface } from '.' +import { BindingPortInterface, BindingsError } from '.' import { BindingInterface, OpenOptions, PortInfo, PortStatus, SetOptions, UpdateOptions } from '@serialport/bindings-interface' import { asyncClose, asyncDrain, asyncFlush, asyncGet, asyncGetBaudRate, asyncList, asyncOpen, asyncRead, asyncSet, asyncUpdate, asyncWrite } from './load-bindings' import { serialNumParser } from './win32-sn-parser' @@ -121,7 +121,7 @@ export class WindowsPortBinding implements BindingPortInterface { return { bytesRead, buffer } } catch (err) { if (!this.isOpen) { - err.canceled = true + throw new BindingsError(err.message, { canceled: true }) } throw err } diff --git a/package-lock.json b/package-lock.json index 0d0cc212..983608b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "hasInstallScript": true, "license": "MIT", "dependencies": { - "@serialport/bindings-interface": "1.1.0", + "@serialport/bindings-interface": "1.2.0", "@serialport/parser-readline": "10.0.1", "debug": "^4.3.2", "node-addon-api": "^4.3.0", @@ -987,9 +987,9 @@ } }, "node_modules/@serialport/bindings-interface": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@serialport/bindings-interface/-/bindings-interface-1.1.0.tgz", - "integrity": "sha512-fIihHyQfA27KHGZg8RslOic53JdU8Xh3e8XHfkSvC1Uq86F6Rk67tfBLbOI4Suh9D1QuSTrLqEozxJAS25dPOQ==", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@serialport/bindings-interface/-/bindings-interface-1.2.0.tgz", + "integrity": "sha512-TLg6z0wyEdfVBIGmRwv0oru7Ijdc3VhH5SDv+q7UXuXvNh+mEpsFedHG205GSGVXbnF1xlCN5cBSMloI74N/gQ==", "engines": { "node": "^12.22 || ^14.13 || >=16" } @@ -13458,9 +13458,9 @@ } }, "@serialport/bindings-interface": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@serialport/bindings-interface/-/bindings-interface-1.1.0.tgz", - "integrity": "sha512-fIihHyQfA27KHGZg8RslOic53JdU8Xh3e8XHfkSvC1Uq86F6Rk67tfBLbOI4Suh9D1QuSTrLqEozxJAS25dPOQ==" + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@serialport/bindings-interface/-/bindings-interface-1.2.0.tgz", + "integrity": "sha512-TLg6z0wyEdfVBIGmRwv0oru7Ijdc3VhH5SDv+q7UXuXvNh+mEpsFedHG205GSGVXbnF1xlCN5cBSMloI74N/gQ==" }, "@serialport/parser-delimiter": { "version": "10.0.1", diff --git a/package.json b/package.json index b7c90684..a629b949 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "UART" ], "dependencies": { - "@serialport/bindings-interface": "1.1.0", + "@serialport/bindings-interface": "1.2.0", "@serialport/parser-readline": "10.0.1", "debug": "^4.3.2", "node-addon-api": "^4.3.0",