com.apple.foundationdb.util.LoggableException Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fdb-extensions Show documentation
Show all versions of fdb-extensions Show documentation
Extensions to the FoundationDB Java API.
/*
* LoggableException.java
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2015-2018 Apple Inc. and the FoundationDB project authors
*
* Licensed 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 com.apple.foundationdb.util;
import com.apple.foundationdb.annotation.API;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;
/**
* Exception type with support for adding keys and values to its log info. This can then
* be logged in a way that better supports searching later.
*/
@SuppressWarnings("serial")
@API(API.Status.MAINTAINED)
public class LoggableException extends RuntimeException implements LoggableKeysAndValues {
@Nonnull private final LoggableKeysAndValuesImpl loggableKeysAndValuesImpl = new LoggableKeysAndValuesImpl();
/**
* Create an exception with the given message a the sequence of key-value pairs.
* This will throw an {@link IllegalArgumentException} if keyValues
* contains an odd number of elements.
*
* @param msg error message
* @param keyValues list
* @see #addLogInfo(Object...)
*/
public LoggableException(@Nonnull String msg, @Nullable Object ... keyValues) {
super(msg);
this.loggableKeysAndValuesImpl.addLogInfo(keyValues);
}
public LoggableException(Throwable cause) {
super(cause);
}
public LoggableException(@Nonnull String msg, @Nullable Throwable cause) {
super(msg, cause);
}
public LoggableException(@Nonnull String msg) {
super(msg);
}
/**
* Get the log information associated with this exception as a map.
*
* @return a single map with all log information
*/
@Nonnull
@Override
public Map getLogInfo() {
return loggableKeysAndValuesImpl.getLogInfo();
}
/**
* Add a key/value pair to the log information. This will use the description
* given as the key and the object provided as the value.
*
* @param description description of the log info pair
* @param object value of the log info pair
* @return this LoggableException
*/
@Nonnull
@Override
public LoggableException addLogInfo(@Nonnull String description, Object object) {
loggableKeysAndValuesImpl.addLogInfo(description, object);
return this;
}
/**
* Add a list of key/value pairs to the log information. This will treat the
* list of items as pairs to be added with every even element being a key
* and every odd element being a value (associated with the even key preceding
* it). So, for example, ["k0", "v0", "k1", "v1"]
will add two
* pairs to the log info, one with key "k0"
and value "v0"
* and one with key "k1"
and value "v1"
. Note that
* this is the same format that is exported by {@link #exportLogInfo()}.
*
* @param keyValue flattened map of key-value pairs
* @return this LoggableException
* @throws IllegalArgumentException if keyValue
has odd length
*/
@Nonnull
@Override
public LoggableException addLogInfo(@Nonnull Object ... keyValue) {
loggableKeysAndValuesImpl.addLogInfo(keyValue);
return this;
}
/**
* Export the log information to a flattened array. This will flatten the map that would
* be returned by {@link #getLogInfo()} into an array where every even element is a key
* within the map and every odd element is the value associated with the key before it. So,
* for example, {"k0:"v0", "k1":"v1"}
would be flattened into
* ["k0", "v0", "k1", "v1"]
. Note that this is the same format that is
* accepted by {@link #addLogInfo(Object...)} and by {@link #LoggableException(String, Object...)}.
*
* @return a flattened map of key-value pairs
*/
@Nonnull
@Override
public Object[] exportLogInfo() {
return loggableKeysAndValuesImpl.exportLogInfo();
}
}