Skip to content

Commit

Permalink
chore(): Delete message in exception constructors (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-sauvee authored Dec 18, 2024
1 parent 695c765 commit ce631cc
Show file tree
Hide file tree
Showing 26 changed files with 118 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ private <T> CloseableResource<T> obtainCloseableResource(Target target, String d
}
});
} catch (InvalidSelectorException e) {
throw new UncheckedJakartaException("Cannot parse selector " + e.getMessage(), e);
throw new UncheckedJakartaException(e);
} catch (NameNotFoundException e) {
throw new UncheckedJakartaException("Cannot find destination " + e.getMessage() + " on jms server " + target.name() + " (" + target.uri().toString() + ")", e);
} catch (NamingException | JMSException e) {
throw new UncheckedJakartaException("Cannot connect to jms server " + target.name() + " (" + target.uri().toString() + "): " + e.getMessage(), e);
throw new UncheckedJakartaException(e, target);
} catch (NamingException e) {
throw new UncheckedJakartaException(e, target);
} catch (JMSException e) {
throw new UncheckedJakartaException(e, target);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,28 @@

package com.chutneytesting.action.jakarta;

import com.chutneytesting.action.spi.injectable.Target;
import jakarta.jms.InvalidSelectorException;
import jakarta.jms.JMSException;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;

@SuppressWarnings("serial")
class UncheckedJakartaException extends RuntimeException {

public UncheckedJakartaException(String message, Exception cause) {
super(message, cause);
public UncheckedJakartaException(InvalidSelectorException e) {
super("Cannot parse selector " + e.getMessage(), e);
}

public UncheckedJakartaException(NameNotFoundException e, Target target) {
super("Cannot find destination " + e.getMessage() + " on jms server " + target.name() + " (" + target.uri().toString() + ")", e);
}

public UncheckedJakartaException(JMSException e, Target target) {
super("Cannot connect to jms server " + target.name() + " (" + target.uri().toString() + "): " + e.getMessage(), e);
}

public UncheckedJakartaException(NamingException e, Target target) {
super("Cannot connect to jms server " + target.name() + " (" + target.uri().toString() + "): " + e.getMessage(), e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ private <T> CloseableResource<T> obtainCloseableResource(Target target, String d
}
});
} catch (InvalidSelectorException e) {
throw new UncheckedJmsException("Cannot parse selector " + e.getMessage(), e);
throw new UncheckedJmsException(e);
} catch (NameNotFoundException e) {
throw new UncheckedJmsException("Cannot find destination " + e.getMessage() + " on jms server " + target.name() + " (" + target.uri().toString() + ")", e);
} catch (NamingException | JMSException e) {
throw new UncheckedJmsException("Cannot connect to jms server " + target.name() + " (" + target.uri().toString() + "): " + e.getMessage(), e);
throw new UncheckedJmsException(e, target);
} catch (NamingException e) {
throw new UncheckedJmsException(e, target);
} catch (JMSException e) {
throw new UncheckedJmsException(e, target);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,28 @@

package com.chutneytesting.action.jms;

import com.chutneytesting.action.spi.injectable.Target;
import javax.jms.InvalidSelectorException;
import javax.jms.JMSException;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;

@SuppressWarnings("serial")
class UncheckedJmsException extends RuntimeException {

public UncheckedJmsException(String message, Exception cause) {
super(message, cause);
public UncheckedJmsException(InvalidSelectorException e) {
super("Cannot parse selector " + e.getMessage(), e);
}

public UncheckedJmsException(NameNotFoundException e, Target target) {
super("Cannot find destination " + e.getMessage() + " on jms server " + target.name() + " (" + target.uri().toString() + ")", e);
}

public UncheckedJmsException(JMSException e, Target target) {
super("Cannot connect to jms server " + target.name() + " (" + target.uri().toString() + "): " + e.getMessage(), e);
}

public UncheckedJmsException(NamingException e, Target target) {
super("Cannot connect to jms server " + target.name() + " (" + target.uri().toString() + "): " + e.getMessage(), e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
@SuppressWarnings("serial")
public class CannotDelegateException extends RuntimeException {

public CannotDelegateException(String message) {
super(message);
public CannotDelegateException(NamedHostAndPort delegate) {
super("Unable to connect to " + delegate.name() + " at " + delegate.host() + ":" + delegate.port());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@

package com.chutneytesting.engine.domain.execution.engine.evaluation;

import org.springframework.expression.ParseException;

@SuppressWarnings("serial")
public class EvaluationException extends RuntimeException {

EvaluationException(String message) {
super(message);
EvaluationException(String expression) {
super("Cannot resolve " + expression + ", Spring evaluation is null");
}

EvaluationException(String expression, Exception e) {
super("Cannot resolve " + expression + " , " + e.getMessage(), e);
}

EvaluationException(String message, Exception cause) {
super(message, cause);
EvaluationException(String expression, ParseException e) {
super("Cannot parse " + expression + " , " + e.getMessage(), e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private Object evaluate(ExpressionParser parser, final EvaluationContext evaluat
try {
Object result = expression.getValue(evaluationContext);
if (result == null) {
throw new EvaluationException("Cannot resolve " + expressionAsString + ", Spring evaluation is null");
throw new EvaluationException(expressionAsString);
}
return result;
} catch (org.springframework.expression.EvaluationException e) {
Expand All @@ -159,7 +159,7 @@ private Object evaluate(ExpressionParser parser, final EvaluationContext evaluat
initialException = (Exception) initialException.getCause();
}
}
throw new EvaluationException("Cannot resolve " + expressionAsString + " , " + initialException.getMessage(), initialException);
throw new EvaluationException(expressionAsString, initialException);
}
}

Expand All @@ -180,7 +180,7 @@ private Expression parseExpression(ExpressionParser parser, String expressionAsS
try {
expression = parser.parseExpression(expressionAsString);
} catch (ParseException e) {
throw new EvaluationException("Cannot parse " + expressionAsString + " , " + e.getMessage(), e);
throw new EvaluationException(expressionAsString, e);
}
return expression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public StepExecutionReport handDown(Step step, NamedHostAndPort delegate) throws
StepExecutionReportDto reportDto = restTemplate.postForObject("https://" + delegate.host() + ":" + delegate.port() + EXECUTION_URL, request, StepExecutionReportDto.class);
return StepExecutionReportMapper.fromDto(reportDto);
} else {
throw new CannotDelegateException("Unable to connect to " + delegate.name() + " at " + delegate.host() + ":" + delegate.port());
throw new CannotDelegateException(delegate);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static EnvironmentBuilder builder() {

Environment addTarget(Target target) {
if (this.containsTarget(target)) {
throw new AlreadyExistingTargetException("Target [" + target.name + "] already exists in [" + this.name + "] environment");
throw new AlreadyExistingTargetException(target.name, this.name);
}

return Environment.builder().from(this).addTarget(target).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public Environment createEnvironment(Environment environment) throws InvalidEnvi

public Environment createEnvironment(Environment environment, boolean force) throws InvalidEnvironmentNameException, AlreadyExistingEnvironmentException {
if (!force && envAlreadyExist(environment)) {
throw new AlreadyExistingEnvironmentException("Environment [" + environment.name + "] already exists");
throw new AlreadyExistingEnvironmentException(environment.name);
}
createOrUpdate(environment);
return environment;
Expand All @@ -82,11 +82,11 @@ public void deleteEnvironment(String environmentName) throws EnvironmentNotFound
List<String> environmentNames = environmentRepository.listNames();
if (environmentNames.stream().noneMatch(env -> env.equals(environmentName))) {
logger.error("Environment not found for name {}", environmentName);
throw new EnvironmentNotFoundException("Environment not found for name " + environmentNames);
throw new EnvironmentNotFoundException(environmentNames);
}
if (environmentNames.size() == 1) {
logger.error("Cannot delete environment with name {} : cannot delete the last env", environmentName);
throw new SingleEnvironmentException("Cannot delete environment with name " + environmentName + " : cannot delete the last env");
throw new SingleEnvironmentException(environmentName);
}
environmentRepository.delete(environmentName);
updateEnvironmentHandlers.forEach(renameEnvironmentHandler -> renameEnvironmentHandler.deleteEnvironment(environmentName));
Expand All @@ -109,10 +109,10 @@ public void updateEnvironment(String environmentName, Environment newVersion) th
public String defaultEnvironmentName() throws EnvironmentNotFoundException {
List<String> envs = environmentRepository.listNames();
if (envs.size() > 1) {
throw new UnresolvedEnvironmentException("There is more than one environment. Could not resolve the default one");
throw new UnresolvedEnvironmentException();
}
if (envs.isEmpty()) {
throw new NoEnvironmentFoundException("No Environment found");
throw new NoEnvironmentFoundException();
}

return envs.get(0);
Expand Down Expand Up @@ -209,7 +209,7 @@ public void deleteVariable(String key, List<String> envs) {
.filter(env -> env.containsVariable(key)).toList();

if (!envs.isEmpty() && environments.isEmpty()) {
throw new EnvVariableNotFoundException("Variable [" + key + "] not found");
throw new EnvVariableNotFoundException(key);
}
environments
.forEach(env -> {
Expand All @@ -228,7 +228,7 @@ private void addVariable(EnvironmentVariable variable, Environment env) throws E

private void createOrUpdate(Environment environment) {
if (!NAME_VALIDATION_PATTERN.matcher(environment.name).matches()) {
throw new InvalidEnvironmentNameException("Environment name must be of 3 to 20 letters, digits, underscore or hyphen");
throw new InvalidEnvironmentNameException();
}
environmentRepository.save(environment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@SuppressWarnings("serial")
public class AlreadyExistingEnvironmentException extends RuntimeException {
public AlreadyExistingEnvironmentException(String message) {
super(message);
public AlreadyExistingEnvironmentException(String environmentName) {
super("Environment [" + environmentName + "] already exists");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
@SuppressWarnings("serial")
public class AlreadyExistingTargetException extends RuntimeException {

public AlreadyExistingTargetException(String message) {
super(message);
public AlreadyExistingTargetException(String targetName, String environmentName) {
super("Target [" + targetName + "] already exists in [" + environmentName + "] environment");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@

package com.chutneytesting.environment.domain.exception;

import java.io.IOException;
import java.nio.file.Path;

@SuppressWarnings("serial")
public class CannotDeleteEnvironmentException extends RuntimeException {

public CannotDeleteEnvironmentException(String message) {
super(message);
}

public CannotDeleteEnvironmentException(String message, Exception cause) {
super(message, cause);
public CannotDeleteEnvironmentException(Path environmentPath, IOException e) {
super("Cannot delete configuration file: " + environmentPath, e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@SuppressWarnings("serial")
public class EnvVariableNotFoundException extends RuntimeException {
public EnvVariableNotFoundException(String message) {
super(message);
public EnvVariableNotFoundException(String variableKey) {
super("Variable [" + variableKey + "] not found");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@

package com.chutneytesting.environment.domain.exception;

import java.nio.file.Path;
import java.util.List;

@SuppressWarnings("serial")
public class EnvironmentNotFoundException extends RuntimeException {
public EnvironmentNotFoundException(String message) {
super(message);
public EnvironmentNotFoundException(Path environmentPath) {
super("Configuration file not found: " + environmentPath);
}

public EnvironmentNotFoundException(List<String> environmentNames) {
super("Environment not found for name " + environmentNames);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@SuppressWarnings("serial")
public class InvalidEnvironmentNameException extends RuntimeException {
public InvalidEnvironmentNameException(String message) {
super(message + ". NOTE: Environment are stored in files, names must be of the form [A-Z0-9_\\-]{3,20}");
public InvalidEnvironmentNameException() {
super("Environment name must be of 3 to 20 letters, digits, underscore or hyphen. NOTE: Environment are stored in files, names must be of the form [A-Z0-9_\\-]{3,20}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@SuppressWarnings("serial")
public class NoEnvironmentFoundException extends RuntimeException {
public NoEnvironmentFoundException(String message) {
super(message);
public NoEnvironmentFoundException() {
super("No Environment found");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@

public class SingleEnvironmentException extends RuntimeException {

public SingleEnvironmentException(String message) {
super(message);
public SingleEnvironmentException(String environmentName) {
super("Cannot delete environment with name " + environmentName + " : cannot delete the last env");
}

public SingleEnvironmentException(String message, Exception cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@SuppressWarnings("serial")
public class UnresolvedEnvironmentException extends RuntimeException {
public UnresolvedEnvironmentException(String message) {
super(message);
public UnresolvedEnvironmentException() {
super("There is more than one environment. Could not resolve the default one");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public synchronized void save(Environment environment) throws UnsupportedOperati
public Environment findByName(String name) throws EnvironmentNotFoundException {
Path environmentPath = getEnvironmentPath(name);
if (!Files.exists(environmentPath)) {
throw new EnvironmentNotFoundException("Configuration file not found: " + environmentPath);
throw new EnvironmentNotFoundException(environmentPath);
}
try {
byte[] bytes = Files.readAllBytes(environmentPath);
Expand Down Expand Up @@ -91,13 +91,13 @@ private boolean isJsonFile(Path path) {
public void delete(String name) {
Path environmentPath = getEnvironmentPath(name);
if (!Files.exists(environmentPath)) {
throw new EnvironmentNotFoundException("Configuration file not found: " + environmentPath);
throw new EnvironmentNotFoundException(environmentPath);
}
try {
Path backupPath = Paths.get(environmentPath.toString() + UUID.randomUUID().getMostSignificantBits() + ".backup");
Files.move(environmentPath, backupPath);
} catch (IOException e) {
throw new CannotDeleteEnvironmentException("Cannot delete configuration file: " + environmentPath, e);
throw new CannotDeleteEnvironmentException(environmentPath, e);
}
}

Expand Down
Loading

0 comments on commit ce631cc

Please sign in to comment.