fix(snmp): resolve TypeScript type errors for Deno strict mode

- Add Buffer import from node:buffer to manager.ts and types.ts
- Fix error handling type assertions (error/retryError/stdError as unknown)
- Add explicit type annotation to byte parameter in isPrintableAscii check
- All tests now passing (3 SNMP tests + 10 logger tests)
This commit is contained in:
2025-10-18 16:17:01 +00:00
parent 1705ffe2be
commit 9ccbbbdc37
2 changed files with 12 additions and 9 deletions

View File

@@ -1,4 +1,5 @@
import * as snmp from "npm:net-snmp@3.20.0";
import { Buffer } from "node:buffer";
import type { IOidSet, ISnmpConfig, TUpsModel, IUpsStatus } from './types.ts';
import { UpsOidSets } from './oid-sets.ts';
@@ -245,7 +246,7 @@ export class NupstSnmp {
// Handle specific types that might need conversion
if (Buffer.isBuffer(value)) {
// If value is a Buffer, try to convert it to a string if it's printable ASCII
const isPrintableAscii = value.every(byte => byte >= 32 && byte <= 126);
const isPrintableAscii = value.every((byte: number) => byte >= 32 && byte <= 126);
if (isPrintableAscii) {
value = value.toString();
}
@@ -335,10 +336,10 @@ export class NupstSnmp {
} catch (error) {
if (this.debug) {
console.error('---------------------------------------');
console.error('Error getting UPS status:', error.message);
console.error('Error getting UPS status:', error instanceof Error ? error.message : String(error));
console.error('---------------------------------------');
}
throw new Error(`Failed to get UPS status: ${error.message}`);
throw new Error(`Failed to get UPS status: ${error instanceof Error ? error.message : String(error)}`);
}
}
@@ -373,7 +374,7 @@ export class NupstSnmp {
return value;
} catch (error) {
if (this.debug) {
console.error(`Error getting ${description}:`, error.message);
console.error(`Error getting ${description}:`, error instanceof Error ? error.message : String(error));
}
// If we're using SNMPv3, try with different security levels
@@ -424,11 +425,11 @@ export class NupstSnmp {
return value;
} catch (retryError) {
if (this.debug) {
console.error(`Retry failed for ${description}:`, retryError.message);
console.error(`Retry failed for ${description}:`, retryError instanceof Error ? retryError.message : String(retryError));
}
}
}
// Try with noAuthNoPriv as a last resort
if (config.securityLevel === 'authPriv' || config.securityLevel === 'authNoPriv') {
const retryConfig = { ...config, securityLevel: 'noAuthNoPriv' as 'noAuthNoPriv' };
@@ -443,11 +444,11 @@ export class NupstSnmp {
return value;
} catch (retryError) {
if (this.debug) {
console.error(`Retry failed for ${description}:`, retryError.message);
console.error(`Retry failed for ${description}:`, retryError instanceof Error ? retryError.message : String(retryError));
}
}
}
return 0;
}
@@ -478,7 +479,7 @@ export class NupstSnmp {
return standardValue;
} catch (stdError) {
if (this.debug) {
console.error(`Standard OID retry failed for ${description}:`, stdError.message);
console.error(`Standard OID retry failed for ${description}:`, stdError instanceof Error ? stdError.message : String(stdError));
}
}