Skip to content

Commit

Permalink
added requestBytes to ModbusCommandError
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeL99 committed Dec 31, 2018
1 parent b96f80c commit 0a4db92
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
24 changes: 19 additions & 5 deletions src/error/error.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
import { ModbusCommandError, ModbusServerError } from './modbus-errors'

describe("Error Tests", () => {
describe('Error Tests', () => {

it("should throw a well formed ModbusCommandError error", done => {
it('should throw a well formed ModbusCommandError error', done => {
try {
// noinspection ExceptionCaughtLocallyJS
throw new ModbusCommandError('Modbus Command Error')
} catch( e) {
} catch (e) {
expect(e).toBeInstanceOf(ModbusCommandError)
expect(e.message).toEqual('Modbus Command Error')
expect(e.toString()).toEqual('ModbusCommandError: Modbus Command Error')
expect(e.requestBytes).toBeUndefined()
done()
}
})

it("should throw a well formed ModbusServerError error", done => {
it('should throw a well formed ModbusCommandError error with requestBytes', done => {
try {
// noinspection ExceptionCaughtLocallyJS
throw new ModbusCommandError('Modbus Command Error', Buffer.from([1, 2, 3, 4]))
} catch (e) {
expect(e).toBeInstanceOf(ModbusCommandError)
expect(e.message).toEqual('Modbus Command Error')
expect(e.toString()).toEqual('ModbusCommandError: Modbus Command Error')
expect(e.requestBytes).toEqual(Buffer.from([1, 2, 3, 4]))
done()
}
})

it('should throw a well formed ModbusServerError error', done => {
try {
// noinspection ExceptionCaughtLocallyJS
throw new ModbusServerError('Modbus Server Error')
} catch( e) {
} catch (e) {
expect(e).toBeInstanceOf(ModbusServerError)
expect(e.message).toEqual('Modbus Server Error')
expect(e.toString()).toEqual('ModbusServerError: Modbus Server Error')
Expand Down
5 changes: 4 additions & 1 deletion src/error/modbus-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ export class ModbusServerError extends Error {

export class ModbusCommandError extends Error {

public readonly requestBytes?: Buffer

// Impossible to get Jest to see super branch as covered, have to ignore whole constructor
/* istanbul ignore next */
constructor(public message: string) {
constructor(public message: string, requestBytes?: Buffer) {
super(message)
this.name = 'ModbusCommandError'
this.message = message
this.stack = (new Error()).stack
this.requestBytes = requestBytes
Object.setPrototypeOf(this, ModbusCommandError.prototype)
}

Expand Down
2 changes: 1 addition & 1 deletion src/modbus-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export abstract class ModbusCommand<T extends ModbusCommand<any>> {
*/
public get responsePacket(): Buffer {
if (!this._responsePacket) {
throw new ModbusCommandError('Tried to read response packet, but success or fail has not been called.')
throw new ModbusCommandError('Tried to read response packet, but success or fail has not been called.', this._rawPacket)
}
return this._responsePacket
}
Expand Down
8 changes: 4 additions & 4 deletions src/tcp/modbus-tcp-command-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export class ModbusTcpCommandFactory extends ModbusCommandFactory {
public fromPacket(packet: Buffer) {
// Minimum Modbus TCP request packet size is 12
if (packet.length < 12) {
throw new ModbusCommandError('Packet length too short')
throw new ModbusCommandError('Packet length too short', packet)
}

const fc = this._functionCodeGetter(packet)
Expand Down Expand Up @@ -254,7 +254,7 @@ export class ModbusTcpCommandFactory extends ModbusCommandFactory {
this._registerLengthGetter)
case ModbusFunctionCode.FORCE_SINGLE_COIL:
if (this._coilStatusGetter(packet) === undefined) {
throw new ModbusCommandError('FORCE_SINGLE_COIL - Invalid coil status received.')
throw new ModbusCommandError('FORCE_SINGLE_COIL - Invalid coil status received.', packet)
}
return new ForceSingleCoilCommand(packet, this._unitIdGetter,
this._functionCodeGetter, this._packetCopySuccessGetter,
Expand All @@ -267,14 +267,14 @@ export class ModbusTcpCommandFactory extends ModbusCommandFactory {
this._registerValueGetter)
case ModbusFunctionCode.FORCE_MULTIPLE_COILS:
if(this._coilStatusesGetter(packet) === undefined){
throw new ModbusCommandError('FORCE_MULTIPLE_COILS - Invalid coil status command received')
throw new ModbusCommandError('FORCE_MULTIPLE_COILS - Invalid coil status command received', packet)
}
return new ForceMultipleCoilsCommand(packet, this._unitIdGetter,
this._functionCodeGetter, this._forceMultipleCoilsSuccessGetter,
this._failureGetter, this._coilAddressGetter,
this._coilLengthGetter, this._coilStatusesGetter)
default:
throw new ModbusCommandError('Function code not implemented')
throw new ModbusCommandError('Function code not implemented', packet)
}
}

Expand Down

0 comments on commit 0a4db92

Please sign in to comment.