You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2023/01/20 12:39:19 UTC

[sling-org-apache-sling-repoinit-parser] branch feature/expose-line-and-column-in-parser-exception created (now c777339)

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a change to branch feature/expose-line-and-column-in-parser-exception
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-repoinit-parser.git


      at c777339  SLING-11758 expose line and column in RepoInitParsingException

This branch includes the following new commits:

     new c777339  SLING-11758 expose line and column in RepoInitParsingException

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[sling-org-apache-sling-repoinit-parser] 01/01: SLING-11758 expose line and column in RepoInitParsingException

Posted by kw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch feature/expose-line-and-column-in-parser-exception
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-repoinit-parser.git

commit c77733958a962d5ed5594ca59f18fc812e72e802
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Fri Jan 20 13:39:13 2023 +0100

    SLING-11758 expose line and column in RepoInitParsingException
---
 .../repoinit/parser/RepoInitParsingException.java  | 31 ++++++++++++++++++++--
 .../parser/impl/RepoInitParserService.java         |  8 +++---
 .../repoinit/parser/test/ParserServiceTest.java    | 28 ++++++++++++++++---
 .../repoinit/parser/test/ParsingErrorsTest.java    |  2 +-
 4 files changed, 59 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/apache/sling/repoinit/parser/RepoInitParsingException.java b/src/main/java/org/apache/sling/repoinit/parser/RepoInitParsingException.java
index fa40e52..1f99925 100644
--- a/src/main/java/org/apache/sling/repoinit/parser/RepoInitParsingException.java
+++ b/src/main/java/org/apache/sling/repoinit/parser/RepoInitParsingException.java
@@ -18,10 +18,37 @@
  */
 package org.apache.sling.repoinit.parser;
 
+import org.osgi.annotation.versioning.ProviderType;
+
+@ProviderType
 public class RepoInitParsingException extends Exception {
     private static final long serialVersionUID = 1L;
 
-    public RepoInitParsingException(String reason, Throwable cause) {
-        super(reason, cause);
+    private final int line;
+    private final int column;
+    public RepoInitParsingException(Throwable cause) {
+        this(cause, -1, -1);
+    }
+    
+    public RepoInitParsingException(Throwable cause, int line, int column) {
+        super(cause);
+        this.line = line;
+        this.column = column;
+    }
+
+    /**
+     * 
+     * @return the line where the issue occurred or -1 if not known
+     */
+    public int getLine() {
+        return line;
+    }
+    
+    /**
+     * 
+     * @return the column where the issue occurred or -1 if not known
+     */
+    public int getColumn() {
+        return column;
     }
 }
diff --git a/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java b/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java
index d6ae70a..ca454b3 100644
--- a/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java
+++ b/src/main/java/org/apache/sling/repoinit/parser/impl/RepoInitParserService.java
@@ -41,9 +41,11 @@ public class RepoInitParserService implements RepoInitParser {
         // in order to avoid parsing problems with trailing comments we add a line feed at the end
         try (final Reader readerWrapper = new AddTailingLinefeedFilterReader(r)) {
             return new RepoInitParserImpl(readerWrapper).parse();
-        } catch ( final IOException | TokenMgrError | ParseException e ) {
-            throw new RepoInitParsingException(e.getMessage(), e);
-        }
+        } catch (ParseException e) {
+            throw new RepoInitParsingException(e, e.currentToken.next.beginLine, e.currentToken.next.beginColumn);
+        } catch (final IOException | TokenMgrError e ) {
+            throw new RepoInitParsingException(e);
+        } 
     }
     
     private static final class AddTailingLinefeedFilterReader extends FilterReader {
diff --git a/src/test/java/org/apache/sling/repoinit/parser/test/ParserServiceTest.java b/src/test/java/org/apache/sling/repoinit/parser/test/ParserServiceTest.java
index 1d7e9f7..6612db8 100644
--- a/src/test/java/org/apache/sling/repoinit/parser/test/ParserServiceTest.java
+++ b/src/test/java/org/apache/sling/repoinit/parser/test/ParserServiceTest.java
@@ -18,6 +18,7 @@
 package org.apache.sling.repoinit.parser.test;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
 
 import java.io.Reader;
 import java.io.StringReader;
@@ -27,20 +28,39 @@ import org.apache.sling.repoinit.parser.RepoInitParsingException;
 import org.apache.sling.repoinit.parser.impl.RepoInitParserService;
 import org.apache.sling.repoinit.parser.operations.CreateServiceUser;
 import org.apache.sling.repoinit.parser.operations.Operation;
+import org.junit.Before;
 import org.junit.Test;
 
 public class ParserServiceTest {
+    private RepoInitParserService service;
+
+    @Before
+    public void setUp() {
+        service = new RepoInitParserService();
+    }
+
     @Test
     public void noErrors() throws RepoInitParsingException {
         final Reader r = new StringReader("create service user foo");
-        List<Operation> operations = new RepoInitParserService().parse(r);
+        List<Operation> operations = service.parse(r);
         assertEquals(1, operations.size());
         assertEquals(CreateServiceUser.class, operations.get(0).getClass());
     }
-    
-    @Test(expected = RepoInitParsingException.class)
+
+    @Test
     public void syntaxError() throws RepoInitParsingException {
         final Reader r = new StringReader("not a valid statement");
-        new RepoInitParserService().parse(r);
+        RepoInitParsingException exception = assertThrows(RepoInitParsingException.class, () -> service.parse(r));
+        assertEquals(1, exception.getLine());
+        assertEquals(1, exception.getColumn());
+    }
+    
+    @Test
+    public void syntaxErrorInSecondLine() throws RepoInitParsingException {
+        final Reader r = new StringReader("create service user foo\n  not a valid statement");
+        RepoInitParsingException exception = assertThrows(RepoInitParsingException.class, () -> service.parse(r));
+        assertEquals(2, exception.getLine());
+        assertEquals(3, exception.getColumn());
+        exception.printStackTrace();
     }
 }
diff --git a/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java b/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java
index 7e63c14..f319901 100644
--- a/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java
+++ b/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java
@@ -42,7 +42,7 @@ public class ParsingErrorsTest {
     private final String input;
     private final Class<? extends Throwable> expected;
     
-    @Parameters
+    @Parameters(name="{0}")
     public static Collection<Object[]> data() {
         @SuppressWarnings("serial")
         final List<Object []> result = new ArrayList<Object []>() {{