How to solve JavaScript Error: "runtime exceeded limit"

While executing the proxy only a few times, i am getting this error. Does anyone know, How canI solve this?

{"fault":{"faultstring":" Javascript runtime exceeded limit of 5,000ms","detail":{"errorcode":"steps.javascript.ScriptExecutionFailed"}}}

What are the best practices while facing this kind of error?

1 2 2,353
2 REPLIES 2

Hi @Dinesh Potti,

The answer is: it depends. You are receiving this error because the policy is running longer than allowed from the current JS policy config.

Couple things:

First, you can increase the timeLimit attribute on the JS Policy and that will allow more time for the JS policy to run. Of course that means that the consuming apps would need to be ok with the additional wait. Please see the setting in this example.

<Javascript async="false" 
    continueOnError="false" enabled="true" timeLimit="7000" 
    name="JavaScript-1">

Second, you can review your code, see what is taking so long, and fix it. Use print("my debug info"); statements to see what is happening inside. For instance, are you parsing a large payload and\or performing a long running loop in your code and that is why it is taking long? Or are you performing a synchronous callout that is taking some time? There can be a few things that cause this type of delay so troubleshoot it and fix it.

Hope this answer helps?

YES.

Usually a JavaScript callout policy SHOULD NOT take more than a few milliseconds. If a JS callout is taking more than 5 seconds, then it is probably doing something unusual. What could that be?

  • a callout to an HTTP service via httpClient. If the remote system doesn't respond quickly, then the JS policy will timeout. The solution is: make the backend respond more quickly.
  • Numerical computation. For example computing signatures using public/private key encryption using the SCJS library for JavaScript. Solution: don't use JavaScript for public key crypto.
  • a tight loop. You have a logical loop in your code. Maybe a for or while loop that does not terminate correctly. Solution: correct your code. External testing of your code would help with this sort of thing.

Parsing strings, applying regular expressions, and doing reformation of JSON blobs, even large ones, will not take 5 seconds. These things will take more like 50ms. So it's unlikely that correct code will cause your JS policy to consume 5 seconds.

The other possibility is that your MP is overloaded, and the long runtime for a JS policy is caused by resource starvation. Your code might be absolutely correct, but even so, it takes a long time to run because the MP is so busy. The solution to that: examine why your MP is so busy in the first place. If you have a very high concurrency API proxy, thousands of transactions per second or more, it may be causing what we call the "noisy neighbor" problem. The load placed on the MP by that other API proxy could cause your JS to run slowly, and timeout. This is unlikely, but it happens.