Troubleshooting and Debugging JSSP Code
There are several techniques and tools that help you when you're developing your code. You can write tests to execute the methods in your Service Type, see the template project in GitHub for an example of a test script. It's usually best to start with a tool like Postman to model out your requests. This gives you immediate feedback about the format of the API requests and the payloads that each method returns.
You can also output console messages in your code; see Console Output Methods for examples of supported methods. When you put console.log methods in your code, you must configure an Azure Storage connection string service key for the Service Instance of your Service Type. This Azure storage account must have a queue named k2-console-logs that captures console messages. You can find the connection string in the Access Keys section of your Azure Storage Account - paste this connection string into the Azure Storage Connection String property when registering the Service Instance. You can view these logs online, but it's easier to use the Microsoft Azure Storage Explorer tool to view the full messages.
Azure Storage Connection String setting in the Service Instance
When writing the code for your Service Type, you can throw errors to surface the error to the product. You can throw an error with as much detail as you want to show in the product. As an example, the following line of code is located in the ELSE block of an IF that evaluates the status of the XHR requests:
Throw errors
throw new Error("Error: Request failed with status " + xhr.status + " Details: " + xhr.responseText);
This results in the following error in Management when executing a method that returns an unhandled error (HTTP response 400).
Console output methods
Method | Notes |
---|---|
console.assert(assertion, message) | Logs a message if the assertion is false. e.g. console.assert(false, "this message is logged because the assertion is false"); |
console.count(label) | Creates a counter on a per label basis. e.g. console.count("myCounter"); |
console.countReset(label) | Resets the counter for a specific label. e.g. console.countReset("myCounter"); |
console.debug(message) | Outputs a debug level message e.g. console.debug("my message"); |
console.error(message) | Outputs a error/exception level message e.g. console.error("my message"); |
console.exception(message) | Same as the console.error() function e.g. same as console.error(message); |
console.info(message) | Outputs a info level message e.g. console.info("my message"); |
console.log(message) | Outputs a standard log message e.g. console.log("my message"); |
console.warn(message) | Outputs a warning level message e.g. console.warn("my warning"); |
console.profile(profileName) | Starts a new timer. e.g. console.profile("myProfile"); |
console.profileEnd(profileName); | Stops a timer with a specific profileName. e.g. console.profileEnd("myProfile"); |
console.time(timerName) | Same functionality as profile(). Starts a new timer. e.g. console.time("myTimer"); |
console.timeEnd(timerName) | Same functionality as profileEnd(). Stops a timer with a specific profileName. e.g. console.time("myTimer"); |