|
| 1 | +/* |
| 2 | + * (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.palantir.conjure.java.api.errors; |
| 18 | + |
| 19 | +import com.palantir.logsafe.Arg; |
| 20 | +import com.palantir.tritium.ids.UniqueIds; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.IdentityHashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Set; |
| 26 | +import javax.annotation.Nullable; |
| 27 | +import org.jetbrains.annotations.VisibleForTesting; |
| 28 | + |
| 29 | +/** |
| 30 | + * This class is a collection of methods useful for creating {@link ServiceException}s and {@link CheckedServiceException}s. |
| 31 | + */ |
| 32 | +final class ServiceExceptionUtils { |
| 33 | + private ServiceExceptionUtils() {} |
| 34 | + |
| 35 | + /** |
| 36 | + * Creates an unmodifiable list from the given array. Null entries are filtered out as unmodifiable lists cannot |
| 37 | + * have null elements. |
| 38 | + * |
| 39 | + * @param elements the array to convert to an unmodifiable list |
| 40 | + * @return an unmodifiable list containing the non-null elements of the array |
| 41 | + * @see <a href="https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Collection.html#unmodview">unmodifiable view</a> |
| 42 | + */ |
| 43 | + static <T> List<T> arrayToUnmodifiableList(T[] elements) { |
| 44 | + if (elements == null || elements.length == 0) { |
| 45 | + return Collections.emptyList(); |
| 46 | + } |
| 47 | + List<T> list = new ArrayList<>(elements.length); |
| 48 | + for (T item : elements) { |
| 49 | + if (item != null) { |
| 50 | + list.add(item); |
| 51 | + } |
| 52 | + } |
| 53 | + return Collections.unmodifiableList(list); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Create a message string that includes the exception name, error type, and all arguments irrespective of log |
| 58 | + * safety. |
| 59 | + * |
| 60 | + * @param exceptionName the name of the exception for which the message is being rendered |
| 61 | + * @param errorType the error type the exception represents |
| 62 | + * @param args the arguments to be included in the message |
| 63 | + * @return a message string that includes the exception name, error type, and arguments |
| 64 | + */ |
| 65 | + static String renderUnsafeMessage(String exceptionName, ErrorType errorType, Arg<?>... args) { |
| 66 | + String message = renderNoArgsMessage(exceptionName, errorType); |
| 67 | + |
| 68 | + if (args == null || args.length == 0) { |
| 69 | + return message; |
| 70 | + } |
| 71 | + |
| 72 | + StringBuilder builder = new StringBuilder(); |
| 73 | + builder.append(message).append(": {"); |
| 74 | + boolean first = true; |
| 75 | + for (Arg<?> arg : args) { |
| 76 | + if (arg == null) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + if (first) { |
| 80 | + first = false; |
| 81 | + } else { |
| 82 | + builder.append(", "); |
| 83 | + } |
| 84 | + builder.append(arg.getName()).append("=").append(arg.getValue()); |
| 85 | + } |
| 86 | + builder.append("}"); |
| 87 | + |
| 88 | + return builder.toString(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Create a message string that includes the exception name and error type, but no arguments. |
| 93 | + * |
| 94 | + * @param exceptionName the name of the exception for which the message is being rendered |
| 95 | + * @param errorType the error type the exception represents |
| 96 | + * @return a message string |
| 97 | + */ |
| 98 | + static String renderNoArgsMessage(String exceptionName, ErrorType errorType) { |
| 99 | + return exceptionName + ": " + errorType.code() + " (" + errorType.name() + ")"; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Finds the errorInstanceId of the most recent cause if present, otherwise generates a new random identifier. Note |
| 104 | + * that this only searches {@link Throwable#getCause() causal exceptions}, not {@link Throwable#getSuppressed() |
| 105 | + * suppressed causes}. |
| 106 | + */ |
| 107 | + static String generateErrorInstanceId(@Nullable Throwable cause) { |
| 108 | + return generateErrorInstanceId(cause, Collections.newSetFromMap(new IdentityHashMap<>())); |
| 109 | + } |
| 110 | + |
| 111 | + @VisibleForTesting |
| 112 | + static String generateErrorInstanceId( |
| 113 | + @Nullable Throwable cause, |
| 114 | + // Guard against cause cycles, see Throwable.printStackTrace(PrintStreamOrWriter) |
| 115 | + Set<Throwable> dejaVu) { |
| 116 | + if (cause == null || !dejaVu.add(cause)) { |
| 117 | + // we don't need cryptographically secure random UUIDs |
| 118 | + return UniqueIds.pseudoRandomUuidV4().toString(); |
| 119 | + } |
| 120 | + if (cause instanceof ServiceException) { |
| 121 | + return ((ServiceException) cause).getErrorInstanceId(); |
| 122 | + } |
| 123 | + // TODO(pm): test that this path is hit. |
| 124 | + if (cause instanceof CheckedServiceException) { |
| 125 | + return ((CheckedServiceException) cause).getErrorInstanceId(); |
| 126 | + } |
| 127 | + if (cause instanceof RemoteException) { |
| 128 | + return ((RemoteException) cause).getError().errorInstanceId(); |
| 129 | + } |
| 130 | + return generateErrorInstanceId(cause.getCause(), dejaVu); |
| 131 | + } |
| 132 | +} |
0 commit comments