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

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

Thingworx set a custom return code and response

rbeck
6-Contributor

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  

ACCEPTED SOLUTION

Accepted Solutions

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
PaiChung
22-Sapphire I
(To:rbeck)

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

rbeck
6-Contributor
(To:PaiChung)

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..." 

PaiChung
22-Sapphire I
(To:rbeck)

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.

rbeck
6-Contributor
(To:PaiChung)

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

PaiChung
22-Sapphire I
(To:rbeck)

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.

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.

rbeck
6-Contributor
(To:TravisPickett)

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 

slangley
23-Emerald II
(To:rbeck)

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


Top Tags