Skip to content
Permalink
Browse files
Added serialization support to all NMS exceptions.
  • Loading branch information
Jim Gomes committed Feb 9, 2010
1 parent a436280 commit d986e687a004b07e6062a8bb868ed673249fd9cf
Show file tree
Hide file tree
Showing 7 changed files with 1,156 additions and 1,018 deletions.
@@ -14,61 +14,95 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Text;
using Apache.NMS;
using Apache.NMS.Stomp.Commands;

namespace Apache.NMS.Stomp
{
/// <summary>
/// Exception thrown when the broker returns an error
/// </summary>
[Serializable]
public class BrokerException : NMSException
{
private BrokerError brokerError = null;

public BrokerException()
: base("Broker failed with missing exception log")
{
}

public BrokerException(BrokerError brokerError)
: this(brokerError, null)
{
}

public BrokerException(BrokerError brokerError, Exception innerException)
: base(brokerError.ExceptionClass + " : " + brokerError.Message + "\n" + StackTraceDump(brokerError.StackTraceElements),
innerException)
{
this.brokerError = brokerError;
}

/// <summary>
/// Exception thrown when the broker returns an error
/// </summary>
public class BrokerException : NMSException
{
private BrokerError brokerError;
#region ISerializable interface implementation
#if !NETCF

/// <summary>
/// Generates a nice textual stack trace
/// </summary>
public static string StackTraceDump(StackTraceElement[] elements)
{
StringBuilder builder = new StringBuilder();
if (elements != null)
{
foreach (StackTraceElement e in elements)
{
builder.Append("\n " + e.ClassName + "." + e.MethodName + "(" + e.FileName + ":" + e.LineNumber + ")");
}
}
return builder.ToString();
}
/// <summary>
/// Initializes a new instance of the BrokerException class with serialized data.
/// Throws System.ArgumentNullException if the info parameter is null.
/// Throws System.Runtime.Serialization.SerializationException if the class name is null or System.Exception.HResult is zero (0).
/// </summary>
/// <param name="info">The SerializationInfo that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The StreamingContext that contains contextual information about the source or destination.</param>
protected BrokerException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
: base(info, context)
{
brokerError = info.GetValue("BrokerException.brokerError", typeof(BrokerError)) as BrokerError;
}

public BrokerException() : base("Broker failed with missing exception log")
{
}
/// <summary>
/// When overridden in a derived class, sets the SerializationInfo
/// with information about the exception.
/// </summary>
/// <param name="info">The SerializationInfo that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The StreamingContext that contains contextual information about the source or destination.</param>
public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("BrokerException.brokerError", brokerError);
}

public BrokerException(BrokerError brokerError) : base(
brokerError.ExceptionClass + " : " + brokerError.Message + "\n" + StackTraceDump(brokerError.StackTraceElements))
{
this.brokerError = brokerError;
}
#endif
#endregion

public BrokerError BrokerError
{
get {
return brokerError;
}
}
/// <summary>
/// Generates a nice textual stack trace
/// </summary>
public static string StackTraceDump(StackTraceElement[] elements)
{
StringBuilder builder = new StringBuilder();
if(elements != null)
{
foreach(StackTraceElement e in elements)
{
builder.Append("\n " + e.ClassName + "." + e.MethodName + "(" + e.FileName + ":" + e.LineNumber + ")");
}
}
return builder.ToString();
}

public virtual string JavaStackTrace
{
get {
return brokerError.StackTrace;
}
}
public BrokerError BrokerError
{
get { return brokerError; }
}

}
public virtual string JavaStackTrace
{
get { return brokerError.StackTrace; }
}
}
}


@@ -20,76 +20,79 @@

namespace Apache.NMS.Stomp.Commands
{
public struct StackTraceElement
{
public string ClassName;
public string FileName;
public string MethodName;
public int LineNumber;
}
public struct StackTraceElement
{
public string ClassName;
public string FileName;
public string MethodName;
public int LineNumber;
}

/// <summary>
/// Represents an exception on the broker
/// </summary>
public class BrokerError : BaseCommand
{
private string message;
private string exceptionClass;
private StackTraceElement[] stackTraceElements = {};
private BrokerError cause;
/// <summary>
/// Represents an exception on the broker
/// </summary>
[Serializable]
public class BrokerError : BaseCommand
{
private string message;
private string exceptionClass;
private StackTraceElement[] stackTraceElements = { };
private BrokerError cause;

public override byte GetDataStructureType()
{
return DataStructureTypes.ErrorType;
}

public string Message
{
get { return message; }
set { message = value; }
}
public override byte GetDataStructureType()
{
return DataStructureTypes.ErrorType;
}

public string ExceptionClass
{
get { return exceptionClass; }
set { exceptionClass = value; }
}
public string Message
{
get { return message; }
set { message = value; }
}

public StackTraceElement[] StackTraceElements
{
get { return stackTraceElements; }
set { stackTraceElements = value; }
}
public string ExceptionClass
{
get { return exceptionClass; }
set { exceptionClass = value; }
}

public BrokerError Cause
{
get { return cause; }
set { cause = value; }
}
public StackTraceElement[] StackTraceElements
{
get { return stackTraceElements; }
set { stackTraceElements = value; }
}

public String StackTrace
{
get {
StringWriter writer = new StringWriter();
PrintStackTrace(writer);
return writer.ToString();
}
}
public BrokerError Cause
{
get { return cause; }
set { cause = value; }
}

public void PrintStackTrace(TextWriter writer)
{
writer.WriteLine(exceptionClass + ": " + message);
for (int i = 0; i < stackTraceElements.Length; i++)
{
StackTraceElement element = stackTraceElements[i];
writer.WriteLine(" at " + element.ClassName + "." + element.MethodName + "(" + element.FileName + ":" + element.LineNumber + ")");
}
if (cause != null)
{
writer.WriteLine("Nested Exception:");
cause.PrintStackTrace(writer);
}
}
}
public String StackTrace
{
get
{
StringWriter writer = new StringWriter();
PrintStackTrace(writer);
return writer.ToString();
}
}

public void PrintStackTrace(TextWriter writer)
{
writer.WriteLine(exceptionClass + ": " + message);
for(int i = 0; i < stackTraceElements.Length; i++)
{
StackTraceElement element = stackTraceElements[i];
writer.WriteLine(" at " + element.ClassName + "." + element.MethodName + "(" + element.FileName + ":" + element.LineNumber + ")");
}

if(cause != null)
{
writer.WriteLine("Nested Exception:");
cause.PrintStackTrace(writer);
}
}
}
}

0 comments on commit d986e68

Please sign in to comment.