mirror of
				https://github.com/Xahau/xahau.js.git
				synced 2025-11-04 04:55:48 +00:00 
			
		
		
		
	fix: better error handling for the binary codec (#2693)
* better error handling * respond to comments * remove --watch
This commit is contained in:
		
							
								
								
									
										2
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							@@ -39,7 +39,7 @@
 | 
				
			|||||||
        "enable": true
 | 
					        "enable": true
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "editor.codeActionsOnSave": {
 | 
					    "editor.codeActionsOnSave": {
 | 
				
			||||||
        "source.fixAll.eslint": true
 | 
					        "source.fixAll.eslint": "explicit"
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "files.insertFinalNewline": true,
 | 
					    "files.insertFinalNewline": true,
 | 
				
			||||||
    "files.trimFinalNewlines": true,
 | 
					    "files.trimFinalNewlines": true,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,9 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
## Unreleased
 | 
					## Unreleased
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Fixed
 | 
				
			||||||
 | 
					* Better error handling/error messages for serialization/deserialization errors.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## 2.0.0 (2024-02-01)
 | 
					## 2.0.0 (2024-02-01)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### BREAKING CHANGES
 | 
					### BREAKING CHANGES
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -144,14 +144,18 @@ class BinaryParser {
 | 
				
			|||||||
    if (type === 0) {
 | 
					    if (type === 0) {
 | 
				
			||||||
      type = this.readUInt8()
 | 
					      type = this.readUInt8()
 | 
				
			||||||
      if (type === 0 || type < 16) {
 | 
					      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) {
 | 
					    if (nth === 0) {
 | 
				
			||||||
      nth = this.readUInt8()
 | 
					      nth = this.readUInt8()
 | 
				
			||||||
      if (nth === 0 || nth < 16) {
 | 
					      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`,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -33,6 +33,9 @@ class Blob extends SerializedType {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (typeof value === 'string') {
 | 
					    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))
 | 
					      return new Blob(hexToBytes(value))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,7 +14,13 @@ const OBJECT_END_MARKER = Uint8Array.from([0xe1])
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
function isObjects(args): args is Array<JsonObject> {
 | 
					function isObjects(args): args is Array<JsonObject> {
 | 
				
			||||||
  return (
 | 
					  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',
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
  )
 | 
					  )
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -238,19 +238,19 @@ function fieldParsingTests() {
 | 
				
			|||||||
  it('Field throws when type code out of range', () => {
 | 
					  it('Field throws when type code out of range', () => {
 | 
				
			||||||
    const parser = makeParser('0101')
 | 
					    const parser = makeParser('0101')
 | 
				
			||||||
    expect(() => parser.readField()).toThrow(
 | 
					    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', () => {
 | 
					  it('Field throws when field code out of range', () => {
 | 
				
			||||||
    const parser = makeParser('1001')
 | 
					    const parser = makeParser('1001')
 | 
				
			||||||
    expect(() => parser.readFieldOrdinal()).toThrow(
 | 
					    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', () => {
 | 
					  it('Field throws when both type and field code out of range', () => {
 | 
				
			||||||
    const parser = makeParser('000101')
 | 
					    const parser = makeParser('000101')
 | 
				
			||||||
    expect(() => parser.readFieldOrdinal()).toThrow(
 | 
					    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', () => {
 | 
					  it('readUIntN', () => {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user