Cannot catch multiple exceptions

Hello,

I made groovy script which is placed to workflow validator. Script reads Team name from component description and validates whether it has valid custom field value.

Team name in description is read based on assumption that team name is followed after “:”.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.opensymphony.workflow.InvalidInputException

INVALID_COMPONENT_TEXT = "Issue cannot be created for this component. Contact administrator!"
 
def componentManager = ComponentManager.instance
def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
def customFieldManager = componentManager.getCustomFieldManager()

// get component description
componentsDescription = issue.getComponents().get(0).getString("description")


// get "Team" custom field object
def cfTeam = customFieldManager.getCustomFieldObjectByName("Team")
def fieldConfig = cfTeam.getRelevantConfig(issue)
//Team in component description is followed after ":"
//compare Team in component description with valid Team custom field options
def option = optionsManager.getOptions(fieldConfig).find {it.value == componentsDescription.split(":")[1]}

//throw exception if Team in component description is not valid Team custom field option
if (!option){
	invalidInputException = new InvalidInputException(INVALID_COMPONENT_TEXT)
}

Script works fine if some text is placed in description after “:” , but NullPointerException appears if create issue screen if description is empty. I tried to catch exception as follows but error appears

Error creating issue: An unknown exception occured executing Validator com.onresolve.jira.groovy.GroovyValidator@1971a8a: root cause: java.lang.NullPointerException

EMPTY_TEAM_IN_COMPONENT_TEXT = "Team is not defined for this components. Contact administrator!"
try {
	componentsDescription.contains("Team:")&& componentsDescription.split(":")[1]
}
catch (NullPointerException npex) {
	invalidInputException = new InvalidInputException(EMPTY_TEAM_IN_COMPONENT_TEXT)
}
catch (PatternSyntaxException psex) {
	invalidInputException = new InvalidInputException(EMPTY_TEAM_IN_COMPONENT_TEXT)
}

Could you please help how to handle string split and contains exceptions?

Thank you in advance.

Best regards, Georgiy


1 answer

1 accepted

This widget could not be displayed.

It doesn’t look like the NullPointerException exception is coming from the split. It’s probably coming later on in your code, when you try to use the results of the split (in this case, it.value). If you could paste more of the code, or more of the stack trace, we could get a better understanding.

Alternatively, if you just want to throw an invalidInputException if there’s no text after the “:”, then you could explicitly check for that and throw an error if you find it.

Exactly...

componentsDescription.contains("Team:")&& componentsDescription.split(":")[1]

Short-circuiting means the split won't happen unless it contains Team:. As Cedric says, if the description doesn't contain : just throw an invalid input exception, rather than trying to catch a NPE and re-throw.

Thank you very much for your advise. Now my final code looks as following and works as needed.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.opensymphony.workflow.InvalidInputException

INVALID_TEAM_TEXT = "Issue cannot be created for this component. Contact administrator!"
EMPTY_TEAM_IN_COMPONENT_TEXT = "Team is not defined for this components. Contact administrator!"
 
def componentManager = ComponentManager.instance
def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
def customFieldManager = componentManager.getCustomFieldManager()

// get component description
componentsDescription = issue.getComponents().get(0).getString("description")


try {
// exception will be thrown if component description is empty
	componentsDescription.contains(":")
	if (componentsDescription.contains(":")){
		// get "Team" custom field object
		def cfTeam = customFieldManager.getCustomFieldObjectByName("Team")
		def fieldConfig = cfTeam.getRelevantConfig(issue)
		//Team in component description is followed after ":"
		//compare Team in component description with valid Team custom field options
		def option = optionsManager.getOptions(fieldConfig).find {it.value == componentsDescription.split(":")[1]}
		if (!option){
			// Team in component description does not have valid value in Team custom field
			invalidInputException = new InvalidInputException(INVALID_TEAM_TEXT)
		}
	}
	else{
		// component description does not contain ":"
		invalidInputException = new InvalidInputException(EMPTY_TEAM_IN_COMPONENT_TEXT)	
	}
}
catch (Exception ex) {
	invalidInputException = new InvalidInputException(EMPTY_TEAM_IN_COMPONENT_TEXT)
}

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

476 views 3 17
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you