Try-Catch包裹循环重试逻辑失效及CFLog无输出问题求助
Let's walk through the problems in your code that are causing errors, missing logs, and unreliable retries:
1. CFLog Isn't Working Because of a Typo
You have a typo in your log filename: file = "loogevent" should be file = "logevent". That's why you're not seeing any log entries—ColdFusion can't write to a file with a misspelled name!
2. Retry Count Is Being Doubled
In your catch block, you're manually incrementing i = i + 1, but your for loop already increments i on each iteration. This means every failed attempt skips an extra retry (e.g., first failure takes i from 1 → 3 instead of 2), so you'll never actually get 10 full retries. Remove that manual increment.
3. Undefined Variable mData Causes Silent Failures
You're trying to append mData to aData, but mData isn't defined anywhere in your code. You meant to append the a object you just created (ArrayAppend(aData, a)). This is likely throwing an error that's either not being caught properly, or your broken log setup is hiding it.
4. Unscoped Loop Variable i
Your for loop uses a global i (no var declaration), which could be modified by other code outside the loop and break your retry logic. Always scope loop variables locally with var.
Fixed Code Example
Here's the corrected version of your code with all these issues addressed:
var aData = []; for (var i = 1; i <= 10; i++) { try { var a = {}; var as = calltoapitoogetdata; // Assuming this is your actual API call function a['count'] = as; ArrayAppend(aData, a); // Fixed: append 'a' instead of undefined 'mData' var retJSON = serializeJSON(aData); writedump(retJSON); break; // Exit loop on success } catch (any e) { // Removed manual i increment—loop handles this cflog(text = "Call failed on attempt #i#. Error: #e.message#", application = true, file = "logevent"); writedump("Retry attempt #i# failed: #e.message#"); } }
Additional Tips
- Add the error message (
e.message) to your log to get more context about why the API call is failing. - If
calltoapitoogetdatais an asynchronous operation, your try/catch won't catch errors thrown later—you'll need to handle async errors with callbacks or promises instead. - Verify that your ColdFusion log directory has write permissions so
cflogcan create the file.
内容的提问来源于stack exchange,提问作者simone

