Till Westmann has uploaded a new change for review. https://asterix-gerrit.ics.uci.edu/1000
Change subject: add error code and string formatting to exception ...................................................................... add error code and string formatting to exception Change-Id: I83941719c6ee0a5a2ce7337b328ad094116fd13f --- A hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/exceptions/ErrorCode.java M hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/exceptions/HyracksDataException.java 2 files changed, 118 insertions(+), 2 deletions(-) git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/00/1000/1 diff --git a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/exceptions/ErrorCode.java b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/exceptions/ErrorCode.java new file mode 100644 index 0000000..1fba4b3 --- /dev/null +++ b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/exceptions/ErrorCode.java @@ -0,0 +1,46 @@ +/* + * 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.exceptions; + +public class ErrorCode extends HyracksException { + public static int UNKNOWN = 0; +} +/* + * 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.exceptions; + +public class ErrorCode extends HyracksException { + public static int UNKNOWN = 0; +} diff --git a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/exceptions/HyracksDataException.java b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/exceptions/HyracksDataException.java index eaf8df9..53c6864 100644 --- a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/exceptions/HyracksDataException.java +++ b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/exceptions/HyracksDataException.java @@ -18,24 +18,71 @@ */ package org.apache.hyracks.api.exceptions; +import java.util.Formatter; + public class HyracksDataException extends HyracksException { private static final long serialVersionUID = 1L; + public static final String HYRACKS = "HYR"; + + private final String component; + private final int errorCode; + private final Object[] params; private String nodeId; public HyracksDataException() { + this(HYRACKS, ErrorCode.UNKNOWN, new Object[0]); } public HyracksDataException(String message) { - super(message); + this(HYRACKS, ErrorCode.UNKNOWN, message); } public HyracksDataException(Throwable cause) { - super(cause); + this(HYRACKS, ErrorCode.UNKNOWN, cause); } public HyracksDataException(String message, Throwable cause) { + this(HYRACKS, ErrorCode.UNKNOWN, message, cause); + } + + public HyracksDataException(String component, int errorCode, Object... params) { + this.component = component; + this.errorCode = errorCode; + this.params = params; + } + + public HyracksDataException(String component, int errorCode, String message, Object... params) { + super(message); + this.component = component; + this.errorCode = errorCode; + this.params = params; + } + + public HyracksDataException(String component, int errorCode, Throwable cause, Object... params) { + super(cause); + this.component = component; + this.errorCode = errorCode; + this.params = params; + } + + public HyracksDataException(String component, int errorCode, String message, Throwable cause, Object... params) { super(message, cause); + this.component = component; + this.errorCode = errorCode; + this.params = params; + } + + public String getComponent() { + return component; + } + + public int getErrorCode() { + return errorCode; + } + + public Object[] getParams() { + return params; } public void setNodeId(String nodeId) { @@ -45,4 +92,27 @@ public String getNodeId() { return nodeId; } + + public String getMessage() { + return formatMessage(component, errorCode, super.getMessage(), params); + } + + /** + * formats a error message + * Example: + * formatMessage(HYRACKS, ErrorCode.UNKNOWN, "%1$s -- %2$s", "one", "two") returns "HYR0000: one -- two" + * + * @param component the software component in which the error originated + * @param errorCode the error code itself + * @param message the user provided error message (a format string as specified in {@link java.util.Formatter}) + * @param params an array of objects taht will be provided to the {@link java.util.Formatter} + * @return the formatted string + */ + public static String formatMessage(String component, int errorCode, String message, Object... params) { + try (Formatter fmt = new Formatter()) { + fmt.format("%1$s%2$04d: ", component, errorCode); + fmt.format(message, params); + return fmt.out().toString(); + } + } } -- To view, visit https://asterix-gerrit.ics.uci.edu/1000 To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I83941719c6ee0a5a2ce7337b328ad094116fd13f Gerrit-PatchSet: 1 Gerrit-Project: asterixdb Gerrit-Branch: master Gerrit-Owner: Till Westmann <[email protected]>
