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