Update RESTCONF error mapping
[netconf.git] / restconf / restconf-common / src / main / java / org / opendaylight / restconf / common / errors / RestconfDocumentedException.java
index 1d7870173b8451f8a7d6d3f56cdd7fb20e346e8d..6bc02fe73372fe1e210b38cfbd6b80d10e8d4d0d 100644 (file)
@@ -21,7 +21,9 @@ import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
 import org.opendaylight.yangtools.yang.common.OperationFailedException;
 import org.opendaylight.yangtools.yang.common.RpcError;
+import org.opendaylight.yangtools.yang.common.YangError;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.codec.YangInvalidValueException;
 
 /**
  * Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
@@ -229,6 +231,21 @@ public class RestconfDocumentedException extends WebApplicationException {
         return obj;
     }
 
+    /**
+     * Throw an instance of this exception if the specified exception has a {@link YangError} attachment.
+     *
+     * @param cause Proposed cause of a RestconfDocumented exception
+     */
+    public static void throwIfYangError(final Throwable cause) {
+        if (cause instanceof YangError) {
+            final YangError error = (YangError) cause;
+            throw new RestconfDocumentedException(cause, new RestconfError(ErrorType.valueOf(error.getErrorType()),
+                // FIXME: this is a special-case until we have YangError.getTag()
+                cause instanceof YangInvalidValueException ? ErrorTag.INVALID_VALUE : ErrorTag.MALFORMED_MESSAGE,
+                    error.getErrorMessage().orElse(null), error.getErrorAppTag().orElse(null)));
+        }
+    }
+
     private static List<RestconfError> convertToRestconfErrors(final Collection<? extends RpcError> rpcErrors) {
         final List<RestconfError> errorList = new ArrayList<>();
         if (rpcErrors != null) {