How to catch an Error of a Scriptrunner post function on Jira Cloud?

Riccardo Micozzi January 20, 2021

Hi,

I'm using a ScriptRunner post function that get the value of a CustomField(text read only) and parse this String in a Json and after that read the content of the Json and do something(it Works).

The problem is when in the customfield is passed a String that is not in the correct format(Json), for example if there isn't the first { or the last } of the Json, in this case the post function Failed and i Get this error:

2021-01-20 09:10:42.847 ERROR - expecting a ',' or a ']',  but got 
the current character of  ':' with an int value of 58  on array index of 1 


The current character read is ':' with an int value of 58
expecting a ',' or a ']',  but got 
the current character of  ':' with an int value of 58  on array index of 1 

line number 1
index number 5
["id":"DefectD1","tipologia":"Consegna","descrizione":"defect"} , {"id":"DefectD1","tipologia":"Installazione","descrizione":"defect"}]
.....^
2021-01-20 09:10:42.906 ERROR - Class: com.adaptavist.sr.cloud.workflow.RunScript, Config: [className:com.adaptavist.sr.cloud.workflow.RunScript, uuid:4b8da94e-8b0e-4dcf-8ade-2e81f299b89f, enabled:true, executionUser:ADD_ON, condition:((Map) issue.fields.issuetype)?.name == 'Drop', description:Script, additionalCode:import groovy.json.*




 

The part of the Code that Parse the String in the Json is this: 

String jsonStr = listaAttributiD 
def res = (new JsonSlurper().parseText(jsonStr)) as Map   //HERE IS WHERE THE ERROR LOADS

 

 There is a way to catch this error and prevent the failed of the Post Function?

 

Thanks

2 answers

2 accepted

0 votes
Answer accepted
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 20, 2021

Hi Ricardo,

I can confirm that ScriptRunner for Jira Cloud uses Groovy so this means you would need to the standard approach in groovy to catch exceptions.

This would mean that you write a try/catch statment in your code in order to handle when the operation fails and in the catch statement if it does you could log a stagement to say it failed or just return so the script does not continue.

I can confirm the page located here explains how try/catch statments work in groovy and you wll be able to use this as a reference gudie to helo to refactor your script.

Regards,

Kristian

0 votes
Answer accepted
Mohamed Benziane
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 20, 2021

Hello @Riccardo Micozzi 

You can do a try and catch for theses line, it will be something like this :

https://www.tutorialspoint.com/groovy/groovy_exception_handling.htm

try {

String jsonStr = listaAttributiD 
def res = (new JsonSlurper().parseText(jsonStr)) as Map

} catch (Error) // not sure about if i can catch the ERROR {

your instruction here}

 

I'm not a groovy expert but i already implemented something like this for one client.

 

Hope this helps

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events