Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Can we throw custom http status code and message using groovy?

Hi,

I am looking to see if i can use groovy code to handle custom error messages like below?

There is a logic in my groovy code where it throws invalid date range if If my date is greater then 90 days. I don't have logic where it also change custom status code/message. I want to see if i can throw status code = 405 and message as = Bad Request. Currently it is throwing 200 OK.

Tagged:

Comments

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,

    I replied to a similar question in an other thread, the way to do is to save the values from your script and return them to a data bank tool so that they can them be used in the return status.

  • Sorry. i lost in my work so could not check again. Thank you for sending this.

  • Thank you for helping. I have gone through your logic for custom status changes. I still see the status as 200 with OK. I can see the custom message and code under body but not under status.

    Status: 200 OK [always shows this]

    it shows as below in body:-

    Invalid date
    Bad Request422

  • i am using postman.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    Please check what was configured in "Options" for "Return Status" those fields need to be parameterized from the example. It sounds like you added the message and code to the response body. Make sure that the extension tool is chained to the "Incoming Transport Header" because if you chain it to the "Outgoing response" then the value will be used as the response.

  • Thank You. I do have a question? Do we need to write code to handle this kind of scenarios all the time? I mean can't we handle without code so it becomes easy to maintain.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,

    Most cases do not require code. In your case you had specific types of date validation that is not part of the product so it needed to be scripted.

  • Thank You. I have a groovy date which checks for date is empty and throws error. I can see the error code throwing. When i try with previous dates then it is showing same error. It should show response if date is < today. How can i handle this if date is null then throw error (happening) and date is previous dates then i should see response with data (currently not happening and always throws same error message) .

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,

    Check you script, I would expect the order or checking would be first look for null or empty then check for date in the past. The fact that it is still show the null date either means the script is matching the null case when it shouldn't or the input isn't what is expected.

  • Thank You. I am checking for both conditions but still not able to resolve.

    Note:- i have both conditions in one response.

    1. Date is Null then working.
    2. Date is Not Null and less then current date then not working.
  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    Can you share your script? If it is similar to the one I previously shared you will probably want to use "minusDays" to get the local date that is seven days in the past. Then you will want to adjust the check of the compare value from "compare < 0" to "compare > 0" is what I think you will want.

    https://www.geeksforgeeks.org/localdate-minusdays-method-in-java-with-examples/

  • Thank You. I am willing to share my script but my company do not allow it. I will try the one you suggested and let you know.

  • I should get response with any given dates if they are previous then today. I tried the logic that you recommended but i am still facing it. What i am trying to find is it should display data for previous dates < today but should throw error if date is null. I tried with both combinations but still facing issue.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    Here is an example, I used the minusDates and changed the compare to be greater than. It returns an error for past dates when I try it. Look at the "error before" responder for correlation script that sets an error for dates that are older than seven days.

  • jithenderreddy
    jithenderreddy Posts: 73
    edited February 2020

    Thank You. Its same code that you provided for null dates and date is greater then 7 days.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    It is very similar just adjust the date back in time and changed one comparison operator.

  • Thank You. I went through your logic in the code. Here i am not using any logic to stop the response. Here the problem is if i provide previous dates then i should get response any error but i am not getting and throwing error which is preventing me to get response data for old dates..

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Are you saying that for old dates you expect a good response? That it is not an error case?

  • Thank You. Yes - we should get response with data and we don't have any error scenario here.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    I think I understand now, for a null of missing date it should return an error but for dates before today it should respond. Here is an example that does that.