Move message formatting to StatementSourceException 28/109628/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 4 Jan 2024 09:53:18 +0000 (10:53 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 4 Jan 2024 14:10:59 +0000 (15:10 +0100)
It would be nice to have consistent formatting relevant things -- and
SourceException already does something nice for typing message and
source together.

JIRA: YANGTOOLS-1150
Change-Id: Ib4b941fe4035edf887d9644a6b68570747b9d937
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
model/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/meta/StatementSourceException.java
parser/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/source/SourceException.java

index 9f056480185d2a1f1aeffcf751f618217d3fabbf..324d16858ab0635b3c216a6aa0c43c4eeb1da121 100644 (file)
@@ -27,20 +27,23 @@ public class StatementSourceException extends RuntimeException {
     private final @NonNull StatementSourceReference sourceRef;
 
     public StatementSourceException(final StatementSourceReference sourceRef, final String message) {
-        super(message);
+        super(createMessage(sourceRef, message));
         this.sourceRef = requireNonNull(sourceRef);
     }
 
     public StatementSourceException(final StatementSourceReference sourceRef, final String message,
             final Throwable cause) {
-        super(message, cause);
+        super(createMessage(sourceRef, message), cause);
         this.sourceRef = requireNonNull(sourceRef);
     }
 
     public StatementSourceException(final StatementSourceReference sourceRef, final String format,
             final Object... args) {
-        super(format.formatted(args));
-        this.sourceRef = requireNonNull(sourceRef);
+        this(sourceRef, format.formatted(args));
+    }
+
+    private static String createMessage(final StatementSourceReference sourceRef, final String message) {
+        return requireNonNull(message) + " [at " + requireNonNull(sourceRef) + ']';
     }
 
     /**
index 7bc37cff85dd0d279371b05e5c696a26d44772a9..ec3ce2ab722031b6e9fb6283e5e75afaab0fca41 100644 (file)
@@ -7,8 +7,6 @@
  */
 package org.opendaylight.yangtools.yang.parser.spi.source;
 
-import static java.util.Objects.requireNonNull;
-
 import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
@@ -31,7 +29,7 @@ public class SourceException extends StatementSourceException {
      * @param sourceRef Statement source
      */
     public SourceException(final @NonNull String message, final @NonNull StatementSourceReference sourceRef) {
-        super(sourceRef, createMessage(message, sourceRef));
+        super(sourceRef, message);
     }
 
     /**
@@ -44,7 +42,7 @@ public class SourceException extends StatementSourceException {
      */
     public SourceException(final @NonNull String message, final @NonNull StatementSourceReference sourceRef,
             final Throwable cause) {
-        super(sourceRef, createMessage(message, sourceRef), cause);
+        super(sourceRef, message, cause);
     }
 
     /**
@@ -228,8 +226,4 @@ public class SourceException extends StatementSourceException {
         throwIf(opt.isEmpty(), stmt, format, args);
         return opt.orElseThrow();
     }
-
-    private static String createMessage(final @NonNull String message, final @NonNull StatementSourceReference source) {
-        return requireNonNull(message) + " [at " + requireNonNull(source) + ']';
-    }
 }