Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle exceptions in Main #21

Merged
merged 1 commit into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 76 additions & 28 deletions src/main/java/org/cqfn/reportwine/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.cqfn.reportwine.converters.YamlToIrConverter;
import org.cqfn.reportwine.converters.YargToDocx4jConverter;
import org.cqfn.reportwine.exceptions.BaseException;
import org.cqfn.reportwine.exceptions.ExpectedSimilarExtensions;
import org.cqfn.reportwine.generators.DocxGenerator;
import org.cqfn.reportwine.generators.PptxGenerator;
import org.cqfn.reportwine.model.CodeHandler;
Expand Down Expand Up @@ -117,11 +118,9 @@ public class Main {
* @param args The command-line arguments
* @throws IOException If an error during input or output actions occurs
* @throws BaseException If an error during a document processing occurs
* @throws Pptx4jException If an error occurs during loading of pptx slides
* @throws Docx4JException If an error occurs during loading of pptx template
*/
public static void main(final String... args)
throws IOException, BaseException, Pptx4jException, Docx4JException {
throws IOException, BaseException {
final Main main = new Main();
final JCommander jcr = JCommander.newBuilder()
.addObject(main)
Expand All @@ -138,38 +137,87 @@ public static void main(final String... args)
* Runs actions.
* @throws IOException If an error during input or output actions occurs
* @throws BaseException If an error during a document processing occurs
* @throws Pptx4jException If an error occurs during loading of pptx slides
* @throws Docx4JException If an error occurs during loading of pptx template
*/
private void run()
throws IOException, BaseException, Pptx4jException, Docx4JException {
final String ext = new ExtensionHandler(this.template, this.output).getExtension();
Pair info = Main.convertYamlToIr(this.project);
if (this.config != null) {
final Pair settings = Main.convertYamlToIr(this.config);
final IrMerger merger = new IrMerger();
info = merger.merge(info, settings);
throws IOException, BaseException {
String ext = "";
try {
ext = new ExtensionHandler(this.template, this.output).getExtension();
} catch (final ExpectedSimilarExtensions exception) {
LOG.severe(exception.getErrorMessage());
}
if (!ext.isEmpty()) {
final BandData data = this.convertYamlToBandData();
switch (ext) {
case "docx":
final DocxGenerator docx = new DocxGenerator(data);
docx.renderDocument(this.template, this.output);
LOG.info("DOCX Report generated");
break;
case "pptx":
final YargToDocx4jConverter docxfj = new YargToDocx4jConverter(data);
PptxGenerator pptx = null;
try {
pptx =
new PptxGenerator(docxfj.convert(), docxfj.getTables());
} catch (final BaseException exception) {
LOG.severe("Cannot cast data to pptx bindings");
}
if (pptx != null) {
try {
pptx.renderDocument(this.template, this.output);
LOG.info("PPTX Report generated");
} catch (final Docx4JException exception) {
LOG.severe("Cannot load pptx template");
} catch (final Pptx4jException exception) {
LOG.severe("Cannot load pptx template slides");
}
}
break;
default:
LOG.info("Report not generated");
break;
}
}
}

/**
* Converts YAML description of a project and additional configurations
* into the intermediate representation, runs scripts to obtain data and
* converts the result into YARG data bindings.
* @return The {@link BandData} object with YARG bindings
* @throws IOException If an error during input or output actions occurs
* @throws BaseException If an error during a document processing occurs
*/
private BandData convertYamlToBandData() throws BaseException, IOException {
Pair info = null;
try {
info = Main.convertYamlToIr(this.project);
if (this.config != null) {
final Pair settings = Main.convertYamlToIr(this.config);
final IrMerger merger = new IrMerger();
info = merger.merge(info, settings);
}
} catch (final BaseException exception) {
LOG.severe("Cannot parse YAML data");
LOG.severe(exception.getErrorMessage());
throw exception;
} catch (final IOException exception) {
LOG.severe("Cannot read YAML file");
throw exception;
}
final CodeHandler handler = new CodeHandler(info);
final Pair replaced = handler.process();
final IrToYargConverter converter = new IrToYargConverter(replaced);
final BandData data = converter.convert();
switch (ext) {
case "docx":
final DocxGenerator docx = new DocxGenerator(data);
docx.renderDocument(this.template, this.output);
LOG.info("DOCX Report generated");
break;
case "pptx":
final YargToDocx4jConverter docxfj = new YargToDocx4jConverter(data);
final PptxGenerator pptx = new PptxGenerator(docxfj.convert(), docxfj.getTables());
pptx.renderDocument(this.template, this.output);
LOG.info("PPTX Report generated");
break;
default:
LOG.info("Report not generated");
break;
BandData data = null;
try {
data = converter.convert();
} catch (final BaseException exception) {
LOG.severe("Cannot cast data to docx bindings");
LOG.severe(exception.getErrorMessage());
throw exception;
}
return data;
}

/**
Expand Down
13 changes: 4 additions & 9 deletions src/test/java/org/cqfn/reportwine/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@
import java.io.IOException;
import java.nio.file.Path;
import org.cqfn.reportwine.exceptions.BaseException;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.pptx4j.Pptx4jException;

/**
* Test for {@link Main} class.
Expand Down Expand Up @@ -77,8 +75,7 @@ void testNoException(@TempDir final Path source) {
boolean caught = false;
try {
Main.main(example);
} catch (final BaseException | IOException | ParameterException
| Pptx4jException | Docx4JException exc) {
} catch (final BaseException | IOException | ParameterException exc) {
caught = true;
}
Assertions.assertFalse(caught);
Expand All @@ -103,8 +100,7 @@ void testNoExceptionWithConfig(@TempDir final Path source) {
boolean caught = false;
try {
Main.main(example);
} catch (final BaseException | IOException | ParameterException
| Pptx4jException | Docx4JException exc) {
} catch (final BaseException | IOException | ParameterException exc) {
caught = true;
}
Assertions.assertFalse(caught);
Expand All @@ -120,8 +116,7 @@ void testWithException() {
boolean caught = false;
try {
Main.main(example);
} catch (final BaseException | IOException | ParameterException
| Pptx4jException | Docx4JException exc) {
} catch (final BaseException | IOException | ParameterException exc) {
caught = true;
}
Assertions.assertTrue(caught);
Expand All @@ -133,7 +128,7 @@ void testWithException() {
*/
@Test
void testWithInvalidYaml(@TempDir final Path source)
throws IOException, Pptx4jException, Docx4JException {
throws IOException {
final String[] example = {
MainTest.TEMPLATE,
MainTest.TESTS_PATH.concat("template.docx"),
Expand Down