"error": "Error during virtual endpoint execution. Contact Administrator for more details."

Hi I’m new to the Tyk world apologies if my questions are very newbie.

I have an api with a virtual endpoint plugin using inline js. What is supposed to happen on my code is to call ABC’s custom endpoint to request for a new token. Do take note that I have tested the ABCEndpoint in postman and it works totally fine. Now I wanted to call the endpoint using the api virtual endpoint of tyk.io.

First I have tested using this inlinejs to see if it really returns the “hello world” and yes it works totally fine.

function myUniqueFunctionName(request, session, config) {
  var responseObject = { 
    Body: "Hello World", 
    Code: 200 
  }
  return TykJsResponse(responseObject, session.meta_data)
}

Now here is the code that I have implemented that returns this “Error during virtual endpoint execution…”. I have omitted the urlABCEndpoint, client_id and client_secret for security purposes.

function myVirtualEndpoint(request, session, config) {
  var urlABCEndpoint = "https://thedummyurl.com/v2/token";
  var payload = {
    grant_type: "client_credentials",
	client_id: "clientidvalue",
	client_secret: "clientsecretkey",
    account_id: "534004151"
  };

  var headers = {
    "Content-Type": "application/json"
  };

  var options = {
    method: "POST",
    body: JSON.stringify(payload),
    headers: headers
  };

  var responseObject = { 
    Body: "success", 
    Code: 200 
  }
  
  var responseErrorObject = { 
    Body: "error", 
    Code: 500 
  }
  return TykMakeHttpRequest(urlABCEndpoint, options)
    .then(function(response) {
      //return response;
	  return TykJsResponse(responseObject, session.meta_data)
    })
    .catch(function(error) {
      //return error;
	  return TykJsResponse(responseErrorObject, session.meta_data);
    });
}

Error on the postman.

In my core settings I have only selected “Authentication Token” as the Authentication mode and that’s just it.

Hopefully anyone can advise me if there’s anything wrong in my code or any extra configuration I need to do. Thank you very much in advance!

Hello @Yakapo07 and welcome to the community.

The error message usually depicts a syntax error in your JavaScript function. From examining your code, it appears the error may be in your .then and .catch.

The JS plugins and virtual endpoints are run in a Sandboxed JSVM powered by Otto engine that is compiled in the Gateway binary. You are limited to the barebones of a scripting language and the JS API functions we provide e.g. no support for ES6 and above.

We have a few examples that show how you can write and construct your syntax here.

Hope it helps

Hi Olu,

First of all thank you very much for your reply.
I have modified my code based on the samples. Now I’m getting 2 different types of error:

1st Error: 502 Bad Gateway
This happens when my responseObject is

var responseObject = {
      Body: JSON.stringify(response),
      Headers: {
        "Content-Type": "application/json"
      }
    }

OR

var responseObject = {
      Body: response,
      Headers: {
        "Content-Type": "application/json"
      }
    }

2nd Error: { “error”: “Error during virtual endpoint execution. Contact Administrator for more details.” }

var responseObject = {
      Body: JSON.parse(response),
      Headers: {
        "Content-Type": "application/json"
      }
    }

Below is my updated code.

function myVirtualHandlerGetHeaders (request, session, config) {
    //Make api call to upstream target
    newRequest = {
      "Method": "POST",
      "Body": {
        "grant_type": "client_credentials",
        "client_id": "l084wof4gdfallf0s",
        "client_secret": "if0zKVgBZZXBiBv8eeI",
        "account_id": "534004151"
        },
      "Headers": {"Content-Type":"application/json"},
      "Domain": "https://hiddenurl-yl3y.auth.marketingcloudapis.com",
      "Resource": "/v2/token",
      "FormData": {}
    };
    rawlog("--- before get to upstream ---")
  response = TykMakeHttpRequest(JSON.stringify(newRequest));
  rawlog("--- After get to upstream ---")
  log("response type: " + typeof response);
  log("response: " + response);

    var responseObject = {
      Body: JSON.parse(response),
      Headers: {
        "Content-Type": "application/json"
      }
    }
      
    rawlog("Virtual Test ended")
    return TykJsResponse(responseObject, session.meta_data)   
  }

Below is the expected response value.

{
    "access_token": "eyJhbGciOiJIUz",
    "token_type": "Bearer",
    "expires_in": 1079
}

May I know what is wrong in my code or did I miss something here?
Also where do I see the execution logs? I wanted to see if it passed through these lines…

rawlog("--- before get to upstream ---")
response = TykMakeHttpRequest(JSON.stringify(newRequest));
rawlog("--- After get to upstream ---")
log("response type: " + typeof response);
log("response: " + response);

Thank you so much in advance for the reply!

Regards,
Ian Jasper

You should be able to messages in the gateway logs. You might have to enable debug mode if I remember correctly.

Errors in the logs most of the times means a syntax error. So my guess is it doesn’t like

Body: JSON.parse(response),

For the first error you might want to add the Code field in the responseObject to verify. I know the value of Body needs to be a string so maybe using a static string to debug might help find the cause.