001/* 002 * Copyright 2008-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2008-2020 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) 2008-2020 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 checked exception types 046 * defined in the LDAP SDK. 047 */ 048@NotExtensible() 049@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 050public abstract class LDAPSDKException 051 extends Exception 052{ 053 /** 054 * The serial version UID for this serializable class. 055 */ 056 private static final long serialVersionUID = 8080186918165352228L; 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 LDAPSDKException(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 LDAPSDKException(final String message, final Throwable cause) 081 { 082 super(message, cause); 083 } 084 085 086 087 /** 088 * Retrieves a string representation of this exception. 089 * 090 * @return A string representation of this exception. 091 */ 092 @Override() 093 public final String toString() 094 { 095 final StringBuilder buffer = new StringBuilder(); 096 toString(buffer); 097 return buffer.toString(); 098 } 099 100 101 102 /** 103 * Appends a string representation of this exception to the provided buffer. 104 * 105 * @param buffer The buffer to which the string representation of this 106 * exception is to be appended. 107 */ 108 public void toString(final StringBuilder buffer) 109 { 110 buffer.append(super.toString()); 111 } 112 113 114 115 /** 116 * Retrieves a string representation of this exception suitable for use in 117 * messages. 118 * 119 * @return A string representation of this exception suitable for use in 120 * messages. 121 */ 122 public String getExceptionMessage() 123 { 124 final boolean includeCause = 125 Boolean.getBoolean(Debug.PROPERTY_INCLUDE_CAUSE_IN_EXCEPTION_MESSAGES); 126 final boolean includeStackTrace = Boolean.getBoolean( 127 Debug.PROPERTY_INCLUDE_STACK_TRACE_IN_EXCEPTION_MESSAGES); 128 129 return getExceptionMessage(includeCause, includeStackTrace); 130 } 131 132 133 134 /** 135 * Retrieves a string representation of this exception suitable for use in 136 * messages. 137 * 138 * @param includeCause Indicates whether to include information about 139 * the cause (if any) in the exception message. 140 * @param includeStackTrace Indicates whether to include a condensed 141 * representation of the stack trace in the 142 * exception message. 143 * 144 * @return A string representation of this exception suitable for use in 145 * messages. 146 */ 147 public String getExceptionMessage(final boolean includeCause, 148 final boolean includeStackTrace) 149 { 150 final StringBuilder buffer = new StringBuilder(); 151 final String message = getMessage(); 152 if ((message == null) || message.isEmpty()) 153 { 154 toString(buffer); 155 } 156 else 157 { 158 buffer.append(message); 159 } 160 161 if (includeStackTrace) 162 { 163 buffer.append(", trace="); 164 StaticUtils.getStackTrace(this, buffer); 165 } 166 else if (includeCause) 167 { 168 final Throwable cause = getCause(); 169 if (cause != null) 170 { 171 buffer.append(", cause="); 172 buffer.append(StaticUtils.getExceptionMessage(cause)); 173 } 174 } 175 176 final String ldapSDKVersionString = ", ldapSDKVersion=" + 177 Version.NUMERIC_VERSION_STRING + ", revision=" + Version.REVISION_ID; 178 if (buffer.indexOf(ldapSDKVersionString) < 0) 179 { 180 buffer.append(ldapSDKVersionString); 181 } 182 183 return buffer.toString(); 184 } 185}