Catch exceptions and log as warning before the interrupt the program flow (i.e. when using -mediainfo and -list with custom --format options that yield empty strings)

This commit is contained in:
Reinhard Pointner 2017-01-05 17:04:40 +08:00
parent c122e1f275
commit ba772e03c8
2 changed files with 25 additions and 12 deletions

View File

@ -56,22 +56,17 @@ public class ArgumentProcessor {
// print episode info
if (args.list) {
List<String> lines = cli.fetchEpisodeList(args.getDatasource(), args.getSearchQuery(), args.getExpressionFormat(), args.getExpressionFilter(), args.getSortOrder(), args.getLanguage().getLocale());
lines.forEach(System.out::println);
return lines.isEmpty() ? 1 : 0;
return print(cli.fetchEpisodeList(args.getDatasource(), args.getSearchQuery(), args.getExpressionFormat(), args.getExpressionFilter(), args.getSortOrder(), args.getLanguage().getLocale()));
}
// print media info
if (args.mediaInfo) {
List<String> lines = cli.getMediaInfo(args.getFiles(true), args.getExpressionFileFilter(), args.getExpressionFormat());
lines.forEach(System.out::println);
return lines.isEmpty() ? 1 : 0;
return print(cli.getMediaInfo(args.getFiles(true), args.getExpressionFileFilter(), args.getExpressionFormat()));
}
// revert files
if (args.revert) {
List<File> files = cli.revert(args.getFiles(false), args.getExpressionFileFilter(), args.getRenameAction());
return files.isEmpty() ? 1 : 0;
return cli.revert(args.getFiles(false), args.getExpressionFileFilter(), args.getRenameAction()).isEmpty() ? 1 : 0;
}
// file operations
@ -103,6 +98,21 @@ public class ArgumentProcessor {
return 0;
}
private int print(List<?> values) {
int lines = 0;
for (int i = 0; i < values.size(); i++) {
try {
System.out.println(values.get(i));
lines++;
} catch (Exception e) {
debug.warning(e::getMessage);
}
}
return lines == 0 ? 1 : 0;
}
public void runScript(CmdlineInterface cli, ArgumentBean args) throws Throwable {
Bindings bindings = new SimpleBindings();
bindings.put(ScriptShell.SHELL_ARGS_BINDING_NAME, args);

View File

@ -995,9 +995,10 @@ public class CmdlineOperations implements CmdlineInterface {
List<Episode> episodes = applyExpressionFilter(service.getEpisodeList(options.get(0), order, locale), filter);
Map<File, Episode> context = new EntryList<File, Episode>(null, episodes);
return episodes.stream().map(episode -> {
return format != null ? format.format(new MediaBindingBean(episode, null, context)) : EpisodeFormat.SeasonEpisode.format(episode);
}).collect(toList());
// lazy format
return new FunctionList<Episode, String>(episodes, e -> {
return format != null ? format.format(new MediaBindingBean(e, null, context)) : EpisodeFormat.SeasonEpisode.format(e);
});
}
@Override
@ -1013,7 +1014,9 @@ public class CmdlineOperations implements CmdlineInterface {
ExpressionFormat formatter = format != null ? format : new ExpressionFormat("{fn} [{resolution} {vc} {channels} {ac} {minutes}m]");
// lazy format
return new FunctionList<File, String>(selection, f -> formatter.format(new MediaBindingBean(xattr.getMetaInfo(f), f)));
return new FunctionList<File, String>(selection, f -> {
return formatter.format(new MediaBindingBean(xattr.getMetaInfo(f), f));
});
}
@Override