Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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

0 votes
Answer accepted

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

Suggest an answer

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

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you