|
| 1 | +/* |
| 2 | + * (c) Copyright 2025 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.logsafe.SafeLoggable; |
| 21 | +import java.util.List; |
| 22 | +import javax.annotation.Nullable; |
| 23 | + |
| 24 | +/* |
| 25 | + * This is identical to ServiceException, but is used in Conjure-generated code to indicate that an exception was thrown |
| 26 | + * from a service endpoint. |
| 27 | + */ |
| 28 | +public abstract class EndpointServiceException extends RuntimeException implements SafeLoggable { |
| 29 | + private static final String EXCEPTION_NAME = "EndpointServiceException"; |
| 30 | + private final ErrorType errorType; |
| 31 | + private final List<Arg<?>> args; // This is an unmodifiable list. |
| 32 | + private final String errorInstanceId; |
| 33 | + private final String unsafeMessage; |
| 34 | + private final String noArgsMessage; |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates a new exception for the given error. All {@link com.palantir.logsafe.Arg parameters} are propagated to |
| 38 | + * clients. |
| 39 | + */ |
| 40 | + public EndpointServiceException(ErrorType errorType, Arg<?>... parameters) { |
| 41 | + this(errorType, null, parameters); |
| 42 | + } |
| 43 | + |
| 44 | + /** As above, but additionally records the cause of this exception. */ |
| 45 | + public EndpointServiceException(ErrorType errorType, @Nullable Throwable cause, Arg<?>... args) { |
| 46 | + super(cause); |
| 47 | + this.errorInstanceId = ServiceExceptionUtils.generateErrorInstanceId(cause); |
| 48 | + this.errorType = errorType; |
| 49 | + this.args = ServiceExceptionUtils.arrayToUnmodifiableList(args); |
| 50 | + this.unsafeMessage = ServiceExceptionUtils.renderUnsafeMessage(EXCEPTION_NAME, errorType, args); |
| 51 | + this.noArgsMessage = ServiceExceptionUtils.renderNoArgsMessage(EXCEPTION_NAME, errorType); |
| 52 | + } |
| 53 | + |
| 54 | + /** The {@link ErrorType} that gave rise to this exception. */ |
| 55 | + public final ErrorType getErrorType() { |
| 56 | + return errorType; |
| 57 | + } |
| 58 | + |
| 59 | + /** A unique identifier for (this instance of) this error. */ |
| 60 | + public final String getErrorInstanceId() { |
| 61 | + return errorInstanceId; |
| 62 | + } |
| 63 | + |
| 64 | + /** A string that includes the exception name, error type, and all arguments irrespective of log-safety. */ |
| 65 | + @Override |
| 66 | + public final String getMessage() { |
| 67 | + // Including all args here since any logger not configured with safe-logging will log this message. |
| 68 | + return unsafeMessage; |
| 69 | + } |
| 70 | + |
| 71 | + /** A string that includes the exception name and error type, without any arguments. */ |
| 72 | + @Override |
| 73 | + public final String getLogMessage() { |
| 74 | + return noArgsMessage; |
| 75 | + } |
| 76 | + |
| 77 | + /** The list of arguments. */ |
| 78 | + @Override |
| 79 | + public final List<Arg<?>> getArgs() { |
| 80 | + return args; |
| 81 | + } |
| 82 | +} |
0 commit comments