enhancement
Add a new built-in TLScript functiontrythat provides try-catch functionality for exception handling in scripts.
Motivation
Currently, TLScript lacks a built-in mechanism for graceful error handling. When a function call fails, the entire script execution stops. A try-catch mechanism would allow scripts to handle errors gracefully and provide fallback behavior.
Specification
The ''try ''function should work as follows:
Syntax:
$argument.try($tryFunction, $catchFunction)
Parameters:
- argument: The value to pass to the try function
- tryFunction: A function that receives the argument and executes the risky operation
- catchFunction: A function that receives the exception and handles the error case
Behavior:
- Pass the argument to the try function and execute it
- If no exception occurs, return the result of the try function
- If an exception occurs, pass the exception to the catch function and return its result
- The try function is only executed when the try method is called
Examples:
// API call with error handling $statusId.try( givenStatusId -> updateStatusById( apiKey: $API_KEY, statusId: $givenStatusId ), error -> { $logFailedRequest({ "success": false, "reason": "rest_error", "message": toString("REST call failed: ", $error), "details": $error, "timestamp": now() }) } ) // Email sending with fallback $emailAddress.try( emailAddr -> sendMail( subject: $subject, to: $emailAddr, body: $emailBody ), mailError -> { log('EMAIL_DELIVERY_FAILED: Error: "{0}" | Email: "{1}"', $mailError, $emailAddress); info("Email delivery failed. Please contact administrator."); } )
Implementation Notes
- Return type should be Object to accommodate any return type from either function
- The try function must receive the argument as parameter to ensure lazy evaluation
- Exception object should be passed to the catch function for error analysis
Test
Should cover:
- Basic try-catch functionality with argument passing
- Exception propagation to catch block with exception details
- Return value handling from both try and catch functions
- Nested try-catch scenarios
- Integration with other TLScript functions and method chaining