Skip to content

Commit

Permalink
handle pattern exception
Browse files Browse the repository at this point in the history
  • Loading branch information
KochTobi committed Jul 11, 2024
1 parent 5c8dcab commit 9134876
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.nio.file.Path;
import java.util.Optional;
import life.qbic.qpostman.common.functions.FileFilter.MalformedPatternException;
import life.qbic.qpostman.common.options.AuthenticationOptions.NoPasswordException;
import life.qbic.qpostman.common.options.SampleIdentifierOptions.IdentityFileEmptyException;
import life.qbic.qpostman.common.options.SampleIdentifierOptions.IdentityFileNotFoundException;
Expand Down Expand Up @@ -49,6 +50,13 @@ private void logError(RuntimeException e) {
log.error(
"Please provide at least 5 letters for your sample identifiers. The following sample identifiers are to short: "
+ toShortSampleIdsException.getIdentifiers());
} else if (e instanceof MalformedPatternException malformedPatternException) {
log.error(
"The pattern %s is malformed:%s".formatted(malformedPatternException.getPatternString(),
malformedPatternException.getErrorDescription()));
log.debug(
"The pattern %s is malformed:%s".formatted(malformedPatternException.getPatternString(),
malformedPatternException.getErrorDescription()), malformedPatternException);
} else {
log.error("Something went wrong. For more detailed output see " + Path.of(LOG_PATH, "postman.log").toAbsolutePath());
log.debug(e.getMessage(), e);
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/life/qbic/qpostman/common/functions/FileFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,31 @@ public FileFilter withPattern(String pattern) {
try {
compiledPattern = Pattern.compile(pattern);
} catch (PatternSyntaxException e) {
log.error("The pattern %s is malformed. %s".formatted(e.getPattern(), e.getMessage()));
System.exit(1);
throw new MalformedPatternException(e, pattern, e.getMessage());
}
return new FileFilter(suffixes, caseSensitive, compiledPattern);
}

public static class MalformedPatternException extends RuntimeException {

private final String patternString;
private final String errorDescription;
public MalformedPatternException(Throwable cause, String patternString,
String errorDescription) {
super(cause);
this.patternString = patternString;
this.errorDescription = errorDescription;
}

public String getPatternString() {
return patternString;
}

public String getErrorDescription() {
return errorDescription;
}
}

private FileFilter(List<String> suffixes, boolean caseSensitive, Pattern pattern) {
this.suffixes = suffixes;
this.caseSensitive = caseSensitive;
Expand Down

0 comments on commit 9134876

Please sign in to comment.