mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 09:36:27 +00:00
add tests for bootloader protocol errors
This commit is contained in:
committed by
David Lechner
parent
3e4ea382e5
commit
7a7eba407f
@@ -77,6 +77,20 @@ export enum ProtectionLevel {
|
||||
Level2 = 0x02,
|
||||
}
|
||||
|
||||
/**
|
||||
* Protocol error. Thrown e.g. when there is a malformed message.
|
||||
*/
|
||||
export class ProtocolError extends Error {
|
||||
/**
|
||||
* Creates a new ProtocolError.
|
||||
* @param message The error message
|
||||
* @param data The bytecodes that caused the error
|
||||
*/
|
||||
constructor(message: string, public data: DataView) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new message to erase the flash memory.
|
||||
*/
|
||||
@@ -192,11 +206,20 @@ export function parseErrorResponse(msg: DataView): Command {
|
||||
assert(msg.getUint8(0) === 5, 'unexpected length');
|
||||
// Error responses are ordered differently compared to command responses.
|
||||
if (msg.getUint8(2) !== ErrorBytecode) {
|
||||
throw Error('expecting error');
|
||||
throw new ProtocolError(
|
||||
`expecting error bytecode 0x05 but got 0x${msg
|
||||
.getUint8(2)
|
||||
.toString(16)
|
||||
.padStart(2, '0')}`,
|
||||
msg,
|
||||
);
|
||||
}
|
||||
if (msg.getUint8(4) !== ErrorCode.UnknownCommand) {
|
||||
// "command not recognized" is only possible error code
|
||||
throw Error('unexpected error code');
|
||||
throw new ProtocolError(
|
||||
`unknown error code: 0x${msg.getUint8(4).toString(16).padStart(2, '0')}`,
|
||||
msg,
|
||||
);
|
||||
}
|
||||
const command = msg.getUint8(3);
|
||||
return command;
|
||||
|
||||
Reference in New Issue
Block a user