From 3135536022e6f7332b9f3a2a67af25fdd8ffceda Mon Sep 17 00:00:00 2001 From: Joe George Date: Fri, 15 Nov 2024 09:54:23 -0500 Subject: [PATCH] JS fixes --- js/src/Ice/Properties.d.ts | 8 ++++++-- js/src/Ice/Properties.js | 12 +++++++++--- js/test/Ice/properties/Client.ts | 13 +++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/js/src/Ice/Properties.d.ts b/js/src/Ice/Properties.d.ts index 2295c1e5671..63bc8a823f0 100644 --- a/js/src/Ice/Properties.d.ts +++ b/js/src/Ice/Properties.d.ts @@ -45,6 +45,7 @@ declare module "ice" { * * @param key - The property key. * @returns The property value interpreted as an integer. + * @throws {@link PropertyException} - Thrown if the property value is not a valid integer. * * @see {@link setProperty} */ @@ -55,6 +56,8 @@ declare module "ice" { * * @param key - The property key. * @returns The property value interpreted as an integer. + * @throws {@link PropertyException} - Thrown if the property is not a known Ice property or the value is + * not a valid integer. * * @see {@link setProperty} */ @@ -64,12 +67,13 @@ declare module "ice" { * Get a property as an integer. If the property is not set, the given default value is returned. * * @param key The property key. - * @param value The default value to use if the property does not exist. + * @param defaultValue The default value to use if the property does not exist. * @returns The property value interpreted as an integer, or the default value. + * @throws {@link PropertyException} - Thrown if the property value is not a valid integer. * * @see {@link setProperty} */ - getPropertyAsIntWithDefault(key: string, value: number): number; + getPropertyAsIntWithDefault(key: string, defaultValue: number): number; /** * Retrieves a property value as a list of strings. The strings must be separated by whitespace or commas. diff --git a/js/src/Ice/Properties.js b/js/src/Ice/Properties.js index 7ac9ab9afaf..058cb83b9e4 100644 --- a/js/src/Ice/Properties.js +++ b/js/src/Ice/Properties.js @@ -64,17 +64,23 @@ export class Properties { let defaultValue = 0; if (defaultValueString != "") { defaultValue = parseInt(defaultValueString); + DEV: console.assert(!isNaN(defaultValue)); } return this.getPropertyAsIntWithDefault(key, defaultValue); } - getPropertyAsIntWithDefault(key, value) { + getPropertyAsIntWithDefault(key, defaultValue) { const pv = this._properties.get(key); if (pv !== undefined) { pv.used = true; - return parseInt(pv.value); - } else { + const value = parseInt(pv.value); + if (isNaN(value)) { + throw new PropertyException(`property '${key}' has an invalid integer value: '${pv.value}'`); + + } return value; + } else { + return defaultValue; } } diff --git a/js/test/Ice/properties/Client.ts b/js/test/Ice/properties/Client.ts index 68b2643dd21..25593e0c124 100644 --- a/js/test/Ice/properties/Client.ts +++ b/js/test/Ice/properties/Client.ts @@ -154,6 +154,19 @@ export class Client extends TestHelper { test(properties.getIceProperty("Ice.MessageSizeMax") == "20"); out.writeLine("ok"); } + + { + out.write("testing that trying to read a non-numeric value as an int throws... "); + const properties = Ice.createProperties(); + try { + properties.setProperty("Foo", "bar"); + properties.getPropertyAsInt("Foo"); + test(false); + } catch (ex) { + test(ex instanceof Ice.PropertyException); + } + out.writeLine("ok"); + } } async run(args: string[]) {