Cannot catch multiple exceptions

Georgiy Senenkov May 3, 2012

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

1 vote
Answer accepted
CEDRIC ZABEL
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 3, 2012

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.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 3, 2012

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.

Georgiy Senenkov May 6, 2012

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
TAGS
AUG Leaders

Atlassian Community Events