Muokkaa

Jaa


Handling errors and warnings in your script

The only time Scripts returns errors is when you add an entity with invalid values. For example, if you try to add a keyword entity with a bid amount that's not valid, the build operation fails and returns one or more errors.

        var operation = adGroup.newKeywordBuilder()
            .withText(keywordText)
            .withCpc(-5)
            .build();

        if (operation.isSuccessful()) {
            var keyword = operation.getResult();
            Logger.log(`Added keyword, ${keyword.getText()}.`);
        }
        else {
            // The bid amount is not valid, so this path executes
            for (var error of operation.getErrors()) {
                Logger.log(error);
            }
        }

However, if you try to update an entity's properties with an invalid value, Scripts does not return an error. Instead, Scripts writes an error message to the Change Log and your code continues executing. For example, the following code attempts to set the ad group's CPC bid. Because the amount is not valid, the call silently fails, the script continues executing, and an error message is written to the change log.

function main() {
    var adGroup = AdsApp.adGroups().get().next();
    adGroup.bidding().setCpc(-5);
}

Note

Although you may be tempted to call getCpc method after setting its value to verify that the update succeeded, don't. Calling the get method after calling the set method degrades performance by eliminating the batch update feature. For more information, see Batching updates in Best practices.

Other errors such as runtime errors or entity retrieval failures cause script execution to stop. When this happens, the error message is written to the Text Log. The following code attempts to call the foo() function but because the function is not defined the script terminates execution.

function main() {
    foo(); // The script stops because foo() is undefined and generates a reference error
    Logger.log('This line is never logged!');
}

You should always review all messages logged to the change log and text log.