Improve RestconfDocumentedException 37/107237/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 3 Aug 2023 16:01:39 +0000 (18:01 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 3 Aug 2023 16:40:14 +0000 (18:40 +0200)
Use instanceof pattern to remove an explicit cast. Also check incoming
errors and do not allocate an ArrayList if the list is null or empty.

Change-Id: I2201438e0fdb11239dfad071987cdc116f748a48
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit e6e2b79e4e644950ac3ea0c16d9fbe5ed9696fac)

restconf/restconf-common/src/main/java/org/opendaylight/restconf/common/errors/RestconfDocumentedException.java

index 44d5c42a1333182989654c751f0fafcec10e0a4a..54f8d3e2406f15938502271f1627dcb015a58f6b 100644 (file)
@@ -241,8 +241,8 @@ public class RestconfDocumentedException extends WebApplicationException {
      * @param cause Proposed cause of a RestconfDocumented exception
      */
     public static void throwIfYangError(final Throwable cause) {
-        if (cause instanceof YangNetconfErrorAware) {
-            throw new RestconfDocumentedException(cause, ((YangNetconfErrorAware) cause).getNetconfErrors().stream()
+        if (cause instanceof YangNetconfErrorAware infoAware) {
+            throw new RestconfDocumentedException(cause, infoAware.getNetconfErrors().stream()
                 .map(error -> new RestconfError(error.type(), error.tag(), error.message(), error.appTag(),
                     // FIXME: pass down error info
                     null, error.path()))
@@ -251,13 +251,14 @@ public class RestconfDocumentedException extends WebApplicationException {
     }
 
     private static List<RestconfError> convertToRestconfErrors(final Collection<? extends RpcError> rpcErrors) {
-        final List<RestconfError> errorList = new ArrayList<>();
-        if (rpcErrors != null) {
-            for (RpcError rpcError : rpcErrors) {
-                errorList.add(new RestconfError(rpcError));
-            }
+        if (rpcErrors == null || rpcErrors.isEmpty()) {
+            return List.of();
         }
 
+        final var errorList = new ArrayList<RestconfError>();
+        for (var rpcError : rpcErrors) {
+            errorList.add(new RestconfError(rpcError));
+        }
         return errorList;
     }