Bug 7146: Propagate exceptions caught in YangStatementSourceImpl's constructors 31/48631/4
authorIgor Foltin <ifoltin@cisco.com>
Wed, 23 Nov 2016 14:09:25 +0000 (15:09 +0100)
committerRobert Varga <nite@hq.sk>
Tue, 6 Dec 2016 09:27:40 +0000 (09:27 +0000)
Exceptions caught in YangStatementSourceImpl's constructors
are currently just logged which may later lead to a NullPointerException
being thrown during yang statement parser parsing cycle.
This patch changes these constructors so that the caught exceptions are
now propagated

Change-Id: I711407f2a3a43a6a069f8706ace1976c1ed70be8
Signed-off-by: Igor Foltin <ifoltin@cisco.com>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YangStatementSourceImpl.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug7146Test.java [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/bugs/bug7146/foo.yang [new file with mode: 0644]

index b721f150af4b50e9968f161fbc059940be7588f4..b3c026a50bf96a28759447527164556cfb5adbf2 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
+import com.google.common.base.Throwables;
 import com.google.common.collect.ImmutableList;
 import java.io.File;
 import java.io.IOException;
@@ -72,8 +73,8 @@ public final class YangStatementSourceImpl implements StatementStreamSource {
         try {
             statementContext = parseYangSource(loadFile(fileName, isAbsolute));
             yangStatementModelParser = new YangStatementParserListenerImpl(sourceName);
-        } catch (Exception e) {
-            logError(e);
+        } catch (IOException | URISyntaxException | YangSyntaxErrorException e) {
+            throw Throwables.propagate(e);
         }
     }
 
@@ -81,19 +82,15 @@ public final class YangStatementSourceImpl implements StatementStreamSource {
         try {
             statementContext = parseYangSource(inputStream);
             yangStatementModelParser = new YangStatementParserListenerImpl(sourceName);
-        } catch (Exception e) {
-            logError(e);
+        } catch (IOException | YangSyntaxErrorException e) {
+            throw Throwables.propagate(e);
         }
     }
 
     public YangStatementSourceImpl(final SourceIdentifier identifier, final YangStatementParser.StatementContext statementContext) {
-        try {
-            this.statementContext = statementContext;
-            this.sourceName = identifier.getName();
-            yangStatementModelParser = new YangStatementParserListenerImpl(sourceName);
-        } catch (Exception e) {
-            logError(e);
-        }
+        this.statementContext = statementContext;
+        this.sourceName = identifier.getName();
+        yangStatementModelParser = new YangStatementParserListenerImpl(sourceName);
     }
 
     @Override
@@ -163,12 +160,4 @@ public final class YangStatementSourceImpl implements StatementStreamSource {
     public String toString() {
         return sourceName;
     }
-
-    private static void logError(final Exception e) {
-        if (e instanceof YangSyntaxErrorException) {
-            LOG.error(((YangSyntaxErrorException) e).getFormattedMessage(), e);
-        } else {
-            LOG.error(e.getMessage(), e);
-        }
-    }
 }
diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug7146Test.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug7146Test.java
new file mode 100644 (file)
index 0000000..70f8c61
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.yangtools.yang.stmt;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
+
+public class Bug7146Test {
+
+    @Test
+    public void shouldFailOnSyntaxError() throws ReactorException {
+        try {
+            StmtTestUtils.parseYangSources(new YangStatementSourceImpl("/bugs/bug7146/foo.yang", false));
+            fail("RuntimeException should have been thrown because of an unknown character in yang module.");
+        } catch (RuntimeException ex) {
+            final Throwable cause = ex.getCause();
+            assertTrue(cause instanceof YangSyntaxErrorException);
+            assertTrue(cause.getMessage().contains("extraneous input '#'"));
+        }
+    }
+}
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug7146/foo.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug7146/foo.yang
new file mode 100644 (file)
index 0000000..a2a8dcb
--- /dev/null
@@ -0,0 +1,9 @@
+module foo {
+    namespace foo-namespace;
+    prefix foo-prefix;
+
+    revision 2016-11-23;
+
+    #
+    container foo-cont {}
+}
\ No newline at end of file