001/* 002 * Copyright 2009-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2009-2024 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2009-2024 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.util; 037 038 039 040import com.unboundid.ldap.sdk.Version; 041 042 043 044/** 045 * This class serves as the base class for all custom runtime exception types 046 * defined in the LDAP SDK. 047 */ 048@NotExtensible() 049@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 050public abstract class LDAPSDKRuntimeException 051 extends RuntimeException 052{ 053 /** 054 * The serial version UID for this serializable class. 055 */ 056 private static final long serialVersionUID = -805259180160427851L; 057 058 059 060 /** 061 * Creates a new instance of this exception with the provided message. 062 * 063 * @param message The message to use for this exception. 064 */ 065 protected LDAPSDKRuntimeException(@NotNull final String message) 066 { 067 super(message); 068 } 069 070 071 072 /** 073 * Creates a new instance of this exception with the provided message and 074 * cause. 075 * 076 * @param message The message to use for this exception. 077 * @param cause The underlying cause for this exception. It may be 078 * {@code null} if no cause is available. 079 */ 080 protected LDAPSDKRuntimeException(@NotNull final String message, 081 @Nullable final Throwable cause) 082 { 083 super(message, cause); 084 } 085 086 087 088 /** 089 * Retrieves a string representation of this exception. 090 * 091 * @return A string representation of this exception. 092 */ 093 @Override() 094 @NotNull() 095 public final String toString() 096 { 097 final StringBuilder buffer = new StringBuilder(); 098 toString(buffer); 099 return buffer.toString(); 100 } 101 102 103 104 /** 105 * Appends a string representation of this exception to the provided buffer. 106 * 107 * @param buffer The buffer to which the string representation of this 108 * exception is to be appended. 109 */ 110 public void toString(@NotNull final StringBuilder buffer) 111 { 112 buffer.append(super.toString()); 113 } 114 115 116 117 /** 118 * Retrieves a string representation of this exception suitable for use in 119 * messages. 120 * 121 * @return A string representation of this exception suitable for use in 122 * messages. 123 */ 124 @NotNull() 125 public String getExceptionMessage() 126 { 127 final boolean includeCause = 128 Boolean.getBoolean(Debug.PROPERTY_INCLUDE_CAUSE_IN_EXCEPTION_MESSAGES); 129 final boolean includeStackTrace = Boolean.getBoolean( 130 Debug.PROPERTY_INCLUDE_STACK_TRACE_IN_EXCEPTION_MESSAGES); 131 132 return getExceptionMessage(includeCause, includeStackTrace); 133 } 134 135 136 137 /** 138 * Retrieves a string representation of this exception suitable for use in 139 * messages. 140 * 141 * @param includeCause Indicates whether to include information about 142 * the cause (if any) in the exception message. 143 * @param includeStackTrace Indicates whether to include a condensed 144 * representation of the stack trace in the 145 * exception message. 146 * 147 * @return A string representation of this exception suitable for use in 148 * messages. 149 */ 150 @NotNull() 151 public String getExceptionMessage(final boolean includeCause, 152 final boolean includeStackTrace) 153 { 154 final StringBuilder buffer = new StringBuilder(); 155 final String message = getMessage(); 156 if ((message == null) || message.isEmpty()) 157 { 158 toString(buffer); 159 } 160 else 161 { 162 buffer.append(message); 163 } 164 165 if (includeStackTrace) 166 { 167 buffer.append(" stackTrace='"); 168 StaticUtils.getStackTrace(this, buffer); 169 } 170 else if (includeCause) 171 { 172 final Throwable cause = getCause(); 173 if (cause != null) 174 { 175 buffer.append(", cause="); 176 buffer.append(StaticUtils.getExceptionMessage(cause)); 177 } 178 } 179 180 final String ldapSDKVersionString = ", ldapSDKVersion=" + 181 Version.NUMERIC_VERSION_STRING + ", revision=" + Version.REVISION_ID; 182 if (buffer.indexOf(ldapSDKVersionString) < 0) 183 { 184 buffer.append(ldapSDKVersionString); 185 } 186 187 return buffer.toString(); 188 } 189}