|  | 
|  | 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 | + | 
|  | 28 | +/** | 
|  | 29 | + * This class is a collection of methods useful for creating {@link ServiceException}s and {@link CheckedServiceException}s. | 
|  | 30 | + */ | 
|  | 31 | +final class ServiceExceptionUtils { | 
|  | 32 | +    private ServiceExceptionUtils() {} | 
|  | 33 | + | 
|  | 34 | +    /** | 
|  | 35 | +     * Creates an unmodifiable list from the given array. Null entries are filtered out as unmodifiable lists cannot | 
|  | 36 | +     * have null elements. | 
|  | 37 | +     * | 
|  | 38 | +     * @param elements the array to convert to an unmodifiable list | 
|  | 39 | +     * @return an unmodifiable list containing the non-null elements of the array | 
|  | 40 | +     * @see <a href="https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Collection.html#unmodview">unmodifiable view</a> | 
|  | 41 | +     */ | 
|  | 42 | +    static <T> List<T> arrayToUnmodifiableList(T[] elements) { | 
|  | 43 | +        if (elements == null || elements.length == 0) { | 
|  | 44 | +            return Collections.emptyList(); | 
|  | 45 | +        } | 
|  | 46 | +        List<T> list = new ArrayList<>(elements.length); | 
|  | 47 | +        for (T item : elements) { | 
|  | 48 | +            if (item != null) { | 
|  | 49 | +                list.add(item); | 
|  | 50 | +            } | 
|  | 51 | +        } | 
|  | 52 | +        return Collections.unmodifiableList(list); | 
|  | 53 | +    } | 
|  | 54 | + | 
|  | 55 | +    /** | 
|  | 56 | +     * Create a message string that includes the exception name, error type, and all arguments irrespective of log | 
|  | 57 | +     * safety. | 
|  | 58 | +     * | 
|  | 59 | +     * @param exceptionName the name of the exception for which the message is being rendered | 
|  | 60 | +     * @param errorType the error type the exception represents | 
|  | 61 | +     * @param args the arguments to be included in the message | 
|  | 62 | +     * @return a message string that includes the exception name, error type, and arguments | 
|  | 63 | +     */ | 
|  | 64 | +    static String renderUnsafeMessage(String exceptionName, ErrorType errorType, Arg<?>... args) { | 
|  | 65 | +        String message = renderNoArgsMessage(exceptionName, errorType); | 
|  | 66 | + | 
|  | 67 | +        if (args == null || args.length == 0) { | 
|  | 68 | +            return message; | 
|  | 69 | +        } | 
|  | 70 | + | 
|  | 71 | +        StringBuilder builder = new StringBuilder(); | 
|  | 72 | +        builder.append(message).append(": {"); | 
|  | 73 | +        boolean first = true; | 
|  | 74 | +        for (Arg<?> arg : args) { | 
|  | 75 | +            if (arg == null) { | 
|  | 76 | +                continue; | 
|  | 77 | +            } | 
|  | 78 | +            if (first) { | 
|  | 79 | +                first = false; | 
|  | 80 | +            } else { | 
|  | 81 | +                builder.append(", "); | 
|  | 82 | +            } | 
|  | 83 | +            builder.append(arg.getName()).append("=").append(arg.getValue()); | 
|  | 84 | +        } | 
|  | 85 | +        builder.append("}"); | 
|  | 86 | + | 
|  | 87 | +        return builder.toString(); | 
|  | 88 | +    } | 
|  | 89 | + | 
|  | 90 | +    /** | 
|  | 91 | +     * Create a message string that includes the exception name and error type, but no arguments. | 
|  | 92 | +     * | 
|  | 93 | +     * @param exceptionName the name of the exception for which the message is being rendered | 
|  | 94 | +     * @param errorType the error type the exception represents | 
|  | 95 | +     * @return a message string | 
|  | 96 | +     */ | 
|  | 97 | +    static String renderNoArgsMessage(String exceptionName, ErrorType errorType) { | 
|  | 98 | +        return exceptionName + ": " + errorType.code() + " (" + errorType.name() + ")"; | 
|  | 99 | +    } | 
|  | 100 | + | 
|  | 101 | +    /** | 
|  | 102 | +     * Finds the errorInstanceId of the most recent cause if present, otherwise generates a new random identifier. Note | 
|  | 103 | +     * that this only searches {@link Throwable#getCause() causal exceptions}, not {@link Throwable#getSuppressed() | 
|  | 104 | +     * suppressed causes}. | 
|  | 105 | +     */ | 
|  | 106 | +    // VisibleForTesting | 
|  | 107 | +    static String generateErrorInstanceId(@Nullable Throwable cause) { | 
|  | 108 | +        return generateErrorInstanceId(cause, Collections.newSetFromMap(new IdentityHashMap<>())); | 
|  | 109 | +    } | 
|  | 110 | + | 
|  | 111 | +    private static String generateErrorInstanceId( | 
|  | 112 | +            @Nullable Throwable cause, | 
|  | 113 | +            // Guard against cause cycles, see Throwable.printStackTrace(PrintStreamOrWriter) | 
|  | 114 | +            Set<Throwable> dejaVu) { | 
|  | 115 | +        if (cause == null || !dejaVu.add(cause)) { | 
|  | 116 | +            // we don't need cryptographically secure random UUIDs | 
|  | 117 | +            return UniqueIds.pseudoRandomUuidV4().toString(); | 
|  | 118 | +        } | 
|  | 119 | +        if (cause instanceof ServiceException) { | 
|  | 120 | +            return ((ServiceException) cause).getErrorInstanceId(); | 
|  | 121 | +        } | 
|  | 122 | +        if (cause instanceof CheckedServiceException) { | 
|  | 123 | +            return ((CheckedServiceException) cause).getErrorInstanceId(); | 
|  | 124 | +        } | 
|  | 125 | +        if (cause instanceof RemoteException) { | 
|  | 126 | +            return ((RemoteException) cause).getError().errorInstanceId(); | 
|  | 127 | +        } | 
|  | 128 | +        return generateErrorInstanceId(cause.getCause(), dejaVu); | 
|  | 129 | +    } | 
|  | 130 | +} | 
0 commit comments