[ASTERIXDB-2030][FAIL] Do not reformat error messages

- user model changes: no
- storage format changes: no
- interface changes: no

Details:
- if the error message is already formatted, just reuse it
- Add test case

Change-Id: Idd922bca36c7b40903c8b7abbe3386fbedd9c77b
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1932
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Contrib: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Yingyi Bu <buyingyi@gmail.com>
diff --git a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/ErrorMessageUtil.java b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/ErrorMessageUtil.java
index 467d148..285e932 100644
--- a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/ErrorMessageUtil.java
+++ b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/ErrorMessageUtil.java
@@ -91,6 +91,10 @@
             if (!NONE.equals(component)) {
                 fmt.format("%1$s%2$04d: ", component, errorCode);
             }
+            // if the message is already formatted, just return it
+            if (!fmt.toString().isEmpty() && message.startsWith(fmt.toString())) {
+                return message;
+            }
             fmt.format(message == null ? "null" : message, (Object[]) params);
             return fmt.out().toString();
         } catch (Exception e) {
diff --git a/hyracks-fullstack/hyracks/hyracks-api/src/test/java/org/apache/hyracks/api/test/HyracksDataExceptionTest.java b/hyracks-fullstack/hyracks/hyracks-api/src/test/java/org/apache/hyracks/api/test/HyracksDataExceptionTest.java
new file mode 100644
index 0000000..23a1caa
--- /dev/null
+++ b/hyracks-fullstack/hyracks/hyracks-api/src/test/java/org/apache/hyracks/api/test/HyracksDataExceptionTest.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.hyracks.api.test;
+
+import org.apache.hyracks.api.exceptions.ErrorCode;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
+import org.apache.hyracks.api.util.ErrorMessageUtil;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class HyracksDataExceptionTest {
+
+    @Test
+    public void returnedMessageWithComponentTest() {
+        HyracksDataException cause = HyracksDataException.create(ErrorCode.ERROR_PROCESSING_TUPLE, 3);
+        HyracksDataException causeWithNodeId = HyracksDataException.create(cause, "nc1");
+        Assert.assertEquals(cause.getMessage(), causeWithNodeId.getMessage());
+    }
+
+    @Test
+    public void returnedMessageWithNoComponentTest() {
+        HyracksDataException cause = new HyracksDataException(ErrorMessageUtil.NONE, ErrorCode.ERROR_PROCESSING_TUPLE,
+                ErrorCode.getErrorMessage(ErrorCode.ERROR_PROCESSING_TUPLE), null, null, 2);
+        HyracksDataException causeWithNodeId = HyracksDataException.create(cause, "nc1");
+        Assert.assertEquals(cause.getMessage(), causeWithNodeId.getMessage());
+    }
+}
\ No newline at end of file