Overview
Multiple issues relate to codec edge cases that fail silently or with unclear errors. This tracking issue proposes enhanced codec error handling and edge case support.
Related Issues
Proposed Architecture
New Error Type
export class BridgeCodecError extends BridgeError {
constructor(
message: string,
public readonly codecPhase: 'encode' | 'decode',
public readonly valueType: string,
options?: ErrorOptions
) {
super(message, options);
}
}
Explicit Edge Case Handlers
// src/utils/codec.ts
function encodeValue(value: unknown): unknown {
// Dict key validation
if (isPlainObject(value)) {
for (const key of Object.keys(value)) {
if (typeof key !== 'string') {
throw new BridgeCodecError(
`Dict keys must be strings, got ${typeof key}`,
'encode',
'dict'
);
}
}
}
// Bytes handling
if (value instanceof Uint8Array || value instanceof ArrayBuffer) {
throw new BridgeCodecError(
'bytes/bytearray must be base64 encoded before sending',
'encode',
'bytes'
);
}
return value;
}
function decodeValue(value: unknown): unknown {
// numpy/pandas scalar handling
if (isNumpyScalar(value)) {
return convertNumpyScalar(value);
}
// Pydantic model handling
if (isPydanticModel(value)) {
try {
return value.model_dump();
} catch (error) {
throw new BridgeCodecError(
'Pydantic model_dump() failed',
'decode',
'pydantic',
{ cause: error }
);
}
}
return value;
}
Environment Validation
function getMaxBytesFromEnv(): number {
const raw = process.env.TYWRAP_CODEC_MAX_BYTES;
if (!raw) return DEFAULT_MAX_BYTES;
const parsed = parseInt(raw, 10);
if (!Number.isFinite(parsed) || parsed <= 0) {
throw new BridgeCodecError(
`Invalid TYWRAP_CODEC_MAX_BYTES: ${raw}`,
'encode',
'config'
);
}
return parsed;
}
Acceptance Criteria
Scope
This fix touches:
src/utils/codec.ts - edge case handling
src/runtime/errors.ts - BridgeCodecError type
runtime/python_bridge.py - Python-side encoding
test/adversarial_playground.test.ts - edge case tests
Overview
Multiple issues relate to codec edge cases that fail silently or with unclear errors. This tracking issue proposes enhanced codec error handling and edge case support.
Related Issues
Proposed Architecture
New Error Type
Explicit Edge Case Handlers
Environment Validation
Acceptance Criteria
Scope
This fix touches:
src/utils/codec.ts- edge case handlingsrc/runtime/errors.ts- BridgeCodecError typeruntime/python_bridge.py- Python-side encodingtest/adversarial_playground.test.ts- edge case tests