cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Thingworx set a custom return code and response

SOLVED
Highlighted
Participant

Thingworx set a custom return code and response

Hello,

 

In a service I need to return an error with status code = 500 and return type JSON.

 

Actually my service returns JSON but the response I really get is "Error executing service getError. Message :: {"test":"error"} - See Script Error Log for more details." and what I want to get is just the JSON {"test":"error"}

 

I succeeded to have this work if I call a service on a resource but as soon as I call the service of the resource from a service of a Thing it doesn't work.

 

Any ideas?

 

Thanks

Best regards  

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Thingworx set a custom return code and response

Hello,

 

It is not possible to specify the HTML return code from within a thing service.  In the case you provided you could do something like the following:

var result = {};
if(code >= 500)
{
    try
    {
        result.message =  Resources["JSONErrorResource"].GetBadRequest(
                          {content: {"plop": "error"}});
        result.status = "200";
    }
    catch( err )
    {
         logger.error( "The following error occured. " + err );
         result.status = "500";
         result.message = "Some Error message here";
    }
}
else
{
    result.status = "200";
    result.message = "ok";
}

This will return JSON in the following format:

{
    status:200 OR 500
    message:OK OR Custom error message OR Service response
}

 The calling application would then be responsible for checking the content returned for the "true" service status code and response message.

View solution in original post

8 REPLIES 8

Re: Thingworx set a custom return code and response

What does your actual code look like that sets the result when you want to return the error?

Re: Thingworx set a custom return code and response

My service in the Thing calls this:

if(code >= 500){
	// result: JSON
var result =  Resources["JSONErrorResource"].GetBadRequest({
	content: {"plop": "error"} /* JSON */
});
} else{
var result = {"plop": "ok"};
}

 Where Resources["JSONErrorResource"].GetBadRequest is a resource I created in Java which throws a InvalidRequestException

 

It was a test because my main purpose remains to reply an error 500 with a JSON content and not a string "Error executing service getError..." 

Re: Thingworx set a custom return code and response

use a logger statement to see what is actually being generated, also maybe add a try catch because perhaps something else is happening to generate an error you aren't expecting.

Re: Thingworx set a custom return code and response

Hello,

 

Where do you suggest to put the log? Because in the java resource I have only one line (the throw Exception) and in the service there is no much code as you can see in my previous post.

 

Thanks

Best regards

Re: Thingworx set a custom return code and response

Just put logger.warn(result); at the very end.

Or logger.debug if you prefer that, but then make sure you change your logging level.

Re: Thingworx set a custom return code and response

Hello,

 

It is not possible to specify the HTML return code from within a thing service.  In the case you provided you could do something like the following:

var result = {};
if(code >= 500)
{
    try
    {
        result.message =  Resources["JSONErrorResource"].GetBadRequest(
                          {content: {"plop": "error"}});
        result.status = "200";
    }
    catch( err )
    {
         logger.error( "The following error occured. " + err );
         result.status = "500";
         result.message = "Some Error message here";
    }
}
else
{
    result.status = "200";
    result.message = "ok";
}

This will return JSON in the following format:

{
    status:200 OR 500
    message:OK OR Custom error message OR Service response
}

 The calling application would then be responsible for checking the content returned for the "true" service status code and response message.

View solution in original post

Re: Thingworx set a custom return code and response

Hello,

 

It is the direction we will take in the project indeed. 

 

However, it's disappointing that we can't do this. I hope this feature will come in a future version of THX.

 

Thank you 

Re: Thingworx set a custom return code and response

Hi @rbeck.

 

Please feel free to post any ideas on our Ideas page.  This will allow others to vote on it and you can monitor it's status for possible inclusion in a future release.

 

Regards.

 

--Sharon

Announcements

Thingworx Navigate content has a new home! Click here to access the new Thingworx Navigate forum! ______________________________