fix: better error handling for the binary codec (#2693)

* better error handling

* respond to comments

* remove --watch
This commit is contained in:
Mayukha Vadari
2024-05-08 12:20:43 -04:00
committed by GitHub
parent 4d6fef597c
commit 212686baae
6 changed files with 23 additions and 7 deletions

View File

@@ -39,7 +39,7 @@
"enable": true
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,

View File

@@ -2,6 +2,9 @@
## Unreleased
### Fixed
* Better error handling/error messages for serialization/deserialization errors.
## 2.0.0 (2024-02-01)
### BREAKING CHANGES

View File

@@ -144,14 +144,18 @@ class BinaryParser {
if (type === 0) {
type = this.readUInt8()
if (type === 0 || type < 16) {
throw new Error('Cannot read FieldOrdinal, type_code out of range')
throw new Error(
`Cannot read FieldOrdinal, type_code ${type} out of range`,
)
}
}
if (nth === 0) {
nth = this.readUInt8()
if (nth === 0 || nth < 16) {
throw new Error('Cannot read FieldOrdinal, field_code out of range')
throw new Error(
`Cannot read FieldOrdinal, field_code ${nth} out of range`,
)
}
}

View File

@@ -33,6 +33,9 @@ class Blob extends SerializedType {
}
if (typeof value === 'string') {
if (!/^[A-F0-9]*$/iu.test(value)) {
throw new Error('Cannot construct Blob from a non-hex string')
}
return new Blob(hexToBytes(value))
}

View File

@@ -14,7 +14,13 @@ const OBJECT_END_MARKER = Uint8Array.from([0xe1])
*/
function isObjects(args): args is Array<JsonObject> {
return (
Array.isArray(args) && (args.length === 0 || typeof args[0] === 'object')
Array.isArray(args) &&
args.every(
(arg) =>
typeof arg === 'object' &&
Object.keys(arg).length === 1 &&
typeof Object.values(arg)[0] === 'object',
)
)
}

View File

@@ -238,19 +238,19 @@ function fieldParsingTests() {
it('Field throws when type code out of range', () => {
const parser = makeParser('0101')
expect(() => parser.readField()).toThrow(
new Error('Cannot read FieldOrdinal, type_code out of range'),
new Error('Cannot read FieldOrdinal, type_code 1 out of range'),
)
})
it('Field throws when field code out of range', () => {
const parser = makeParser('1001')
expect(() => parser.readFieldOrdinal()).toThrow(
new Error('Cannot read FieldOrdinal, field_code out of range'),
new Error('Cannot read FieldOrdinal, field_code 1 out of range'),
)
})
it('Field throws when both type and field code out of range', () => {
const parser = makeParser('000101')
expect(() => parser.readFieldOrdinal()).toThrow(
new Error('Cannot read FieldOrdinal, type_code out of range'),
new Error('Cannot read FieldOrdinal, type_code 1 out of range'),
)
})
it('readUIntN', () => {