From 80f9c2a8888260cbf2e737f819605f1f05dbccbc Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Fri, 15 Mar 2024 12:17:15 +0300 Subject: [PATCH] fix(#2938): exception --- .../main/java/org/eolang/maven/PhiMojo.java | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/PhiMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/PhiMojo.java index 095a6ed43f..0ececf9bd4 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/PhiMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/PhiMojo.java @@ -34,6 +34,7 @@ import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.List; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; @@ -124,7 +125,19 @@ public void exec() { String.format(".%s", PhiMojo.EXT) ) ); - home.save(PhiMojo.translated(train, xml), relative); + try { + home.save(PhiMojo.translated(train, xml), relative); + } catch (final ImpossibleToPhiTranslationException exception) { + Logger.error( + this, + "XML is not translatable to phi:\n%s", + xml.toString() + ); + throw new IllegalStateException( + String.format("Couldn't translate %s to phi", processed), + exception + ); + } Logger.info( this, "Translated to phi: %[file]s (%[size]s) -> %[file]s (%[size]s)", @@ -158,12 +171,29 @@ count, new Rel(this.phiInputDir), new Rel(this.phiOutputDir) * @param xmir Text of xmir * @return Translated xmir */ - private static String translated(final Train train, final XML xmir) { - return new Xsline( + private static String translated(final Train train, final XML xmir) throws ImpossibleToPhiTranslationException { + final List translated = new Xsline( train.with(new StClasspath("/org/eolang/maven/phi/to-phi.xsl")) - ) - .pass(xmir) - .xpath("phi/text()") - .get(0); + ).pass(xmir).xpath("phi/text()"); + if (translated.isEmpty()) { + throw new ImpossibleToPhiTranslationException( + "Xpath 'phi/text()' is not found in translated XMIR" + ); + } + return translated.get(0); + } + + /** + * Exception which indicates that translation to phi can't be processed. + * @since 0.36.0 + */ + static class ImpossibleToPhiTranslationException extends Exception { + /** + * Ctor. + * @param cause Cause of the exception. + */ + ImpossibleToPhiTranslationException(final String cause) { + super(cause); + } } }