-
Notifications
You must be signed in to change notification settings - Fork 0
Troubleshooting
Common issues and solutions when using Phantom.js.
This generic error occurs when:
-
Invalid input to number operations
// Problem: Trying to parse non-numeric value phantom.numbers.operation.parse("abc"); // Solution: Validate first if (phantom.numbers.operation.isNumber("abc")) { var num = phantom.numbers.operation.parse("abc"); }
-
Division by zero
// Problem: Dividing by zero phantom.numbers.operation.divide(10, 0); // Solution: Check denominator first if (!phantom.numbers.operation.isZero(denominator)) { var result = phantom.numbers.operation.divide(10, denominator); }
-
Null/undefined in strict operations
// Problem: Null value in strict operation phantom.numbers.operation.parse(null); // Solution: Check for null/undefined if (value != null) { var num = phantom.numbers.operation.parse(value); }
-
Operations on read-only maps
// Problem: Trying to save to configuration map phantom.maps.configuration.save("key", "value"); // Solution: Configuration map is read-only, use get() only var value = phantom.maps.configuration.get("key");
-
Null key in map operations
// Problem: Null key phantom.maps.channel.save(null, "value"); // Solution: Ensure key is not null if (key != null) { phantom.maps.channel.save(key, "value"); }
Problem: Getting null when retrieving from map.
var value = phantom.maps.channel.get("key");
// value is nullSolutions:
-
Check if key exists first:
if (phantom.maps.channel.exists("key")) { var value = phantom.maps.channel.get("key"); }
-
Provide default value:
var value = phantom.maps.channel.get("key") || "default";
Problem: Getting error when using response map.
phantom.maps.response.save("key", "value");
// Error: "Invalid operation"Solution: Response map is only available in response context. Check context:
try {
phantom.maps.response.save("key", "value");
} catch (e) {
// Not in response context, use alternative map
phantom.maps.channel.save("key", "value");
}Problem: Operations on null/undefined return empty strings.
phantom.strings.operation.trim(null);
// Returns: "" (empty string, not error)Solution: This is expected behavior. Check for empty strings:
var result = phantom.strings.operation.trim(input);
if (!phantom.strings.operation.isEmpty(result)) {
// Process result
}Problem: replace only replaces first occurrence.
phantom.strings.operation.replace("hello hello", "hello", "hi");
// Returns: "hi hello" (only first replaced)Solution: Use replaceAll for all occurrences:
phantom.strings.operation.replaceAll("hello hello", "hello", "hi");
// Returns: "hi hi"Problem: isNumber(null) returns true because Number(null) = 0.
phantom.numbers.operation.isNumber(null);
// Returns: trueSolution: Check for null explicitly if needed:
if (value != null && phantom.numbers.operation.isNumber(value)) {
var num = phantom.numbers.operation.parse(value);
}Problem: Decimal calculations may have precision issues.
phantom.numbers.operation.add(0.1, 0.2);
// Returns: 0.30000000000000004Solution: Round results when needed:
var result = phantom.numbers.operation.round(
phantom.numbers.operation.add(0.1, 0.2),
2
);
// Returns: 0.3Problem: Random numbers seem predictable.
Solution: This is expected - JavaScript's Math.random() is pseudo-random. For OIE use cases, this is typically sufficient.
Problem: Operations seem slow.
Solutions:
-
Avoid redundant operations:
// Bad: Multiple trims var a = phantom.strings.operation.trim(input); var b = phantom.strings.operation.trim(input); // Good: Store result var trimmed = phantom.strings.operation.trim(input); var a = trimmed; var b = trimmed;
-
Validate early to avoid unnecessary processing:
if (phantom.strings.operation.isBlank(input)) { return; // Exit early } // Process only if valid
var step1 = phantom.strings.operation.trim(input);
logger.info("Step 1: " + step1);
var step2 = phantom.strings.operation.toUpperCase(step1);
logger.info("Step 2: " + step2);if (phantom.maps.channel.exists("key")) {
var value = phantom.maps.channel.get("key");
logger.info("Map value: " + value);
}var input = phantom.maps.channel.get("input");
logger.info("Input type: " + typeof input);
logger.info("Input value: " + input);
if (phantom.numbers.operation.isNumber(input)) {
var num = phantom.numbers.operation.parse(input);
logger.info("Parsed number: " + num);
}If you encounter issues not covered here:
- Check the API Reference for correct usage
- Review Examples for similar use cases
- Check Best Practices for patterns
- Open an issue on the GitHub repository
- Best Practices - Tips to avoid common issues
- Examples - Working examples
- API Reference - Complete API documentation
Phantom - A product of David Labs.