Skip to content

Commit

Permalink
feat: BindingsError with a canceled property
Browse files Browse the repository at this point in the history
  • Loading branch information
reconbot committed Feb 1, 2022
1 parent b50165b commit 53b5c25
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 21 deletions.
3 changes: 2 additions & 1 deletion lib/bindings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
})
Expand Down
10 changes: 6 additions & 4 deletions lib/errors.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
4 changes: 2 additions & 2 deletions lib/poller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lib/unix-read.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BindingsError } from '.'
import { assert, shouldReject } from '../test/assert'
import { LinuxPortBinding } from './linux'
import { unixRead } from './unix-read'
Expand Down Expand Up @@ -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 () => {
Expand Down
6 changes: 3 additions & 3 deletions lib/unix-read.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/win32.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 53b5c25

Please sign in to comment.