add different error classes

This commit is contained in:
2024-10-07 12:55:48 -05:00
parent eae4ef7256
commit 753ec6d8df
+5 -9
View File
@@ -11,7 +11,7 @@ const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
} }
return Buffer.from(oscString, 'ascii'); return Buffer.from(oscString, 'ascii');
} }
throw new Error('osc type s toBuffer called with non string value'); throw new TypeError('osc type s toBuffer called with non string value');
}, },
fromString: (string: string) => string, fromString: (string: string) => string,
fromBuffer: (bytes: Buffer) => { fromBuffer: (bytes: Buffer) => {
@@ -40,7 +40,7 @@ const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
buffer.writeFloatBE(number); buffer.writeFloatBE(number);
return buffer; return buffer;
} }
throw new Error('osc type f toBuffer called with non number value'); throw new TypeError('osc type f toBuffer called with non number value');
}, },
fromString: (string: string) => Number.parseFloat(string), fromString: (string: string) => Number.parseFloat(string),
fromBuffer: (buffer) => { fromBuffer: (buffer) => {
@@ -59,7 +59,7 @@ const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
buffer.writeInt32BE(number); buffer.writeInt32BE(number);
return buffer; return buffer;
} }
throw new Error('osc type i toBuffer called with non number value'); throw new TypeError('osc type i toBuffer called with non number value');
}, },
fromString: (string: string) => Number.parseInt(string, 10), fromString: (string: string) => Number.parseInt(string, 10),
fromBuffer: (buffer) => { fromBuffer: (buffer) => {
@@ -81,7 +81,7 @@ const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
return Buffer.concat([sizeBuffer, data, padBuffer]); return Buffer.concat([sizeBuffer, data, padBuffer]);
} }
} }
throw new Error('osc type b toBuffer called with non Buffer value'); throw new TypeError('osc type b toBuffer called with non Buffer value');
}, },
fromString: (string: string) => Buffer.from(string, 'hex'), fromString: (string: string) => Buffer.from(string, 'hex'),
fromBuffer: (buffer) => { fromBuffer: (buffer) => {
@@ -126,11 +126,7 @@ function argsToBuffer(args: OSCArg[]) {
const arg = args[index]; const arg = args[index];
const typeConverter = oscTypeConverterMap[arg.type]; const typeConverter = oscTypeConverterMap[arg.type];
if (typeConverter === undefined) { if (typeConverter === undefined) {
throw new Error('osc type error: unknown type '.concat(arg.type)); throw new TypeError('unknown type '.concat(arg.type));
}
if (typeConverter.fromString === undefined) {
throw new Error('osc type error: no string converter for type '.concat(arg.type));
} }
const buffer = typeConverter.toBuffer(arg.value); const buffer = typeConverter.toBuffer(arg.value);