From afc7b6cfbb425f56948976ed6b3b7edb927527cf Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 16 Oct 2001 16:44:20 +0000 Subject: [PATCH] Turned ObjectStreamException into a nested exception so that we can provide information on what exception caused the object serialization or unserialization to freak out. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@464 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../presents/dobj/io/DObjectFactory.java | 6 +- .../presents/io/ObjectStreamException.java | 99 ++++++++++++++++++- .../presents/io/TypedObjectFactory.java | 10 +- 3 files changed, 103 insertions(+), 12 deletions(-) diff --git a/src/java/com/threerings/presents/dobj/io/DObjectFactory.java b/src/java/com/threerings/presents/dobj/io/DObjectFactory.java index 190568bf6..cd02d9084 100644 --- a/src/java/com/threerings/presents/dobj/io/DObjectFactory.java +++ b/src/java/com/threerings/presents/dobj/io/DObjectFactory.java @@ -1,5 +1,5 @@ // -// $Id: DObjectFactory.java,v 1.8 2001/10/11 04:07:52 mdb Exp $ +// $Id: DObjectFactory.java,v 1.9 2001/10/16 16:44:20 mdb Exp $ package com.threerings.presents.dobj.io; @@ -59,8 +59,8 @@ public class DObjectFactory return dobj; } catch (Exception e) { - String errmsg = "Unable to unserialize dobj: " + e; - throw new ObjectStreamException(errmsg); + String errmsg = "Failure unserializing dobj"; + throw new ObjectStreamException(errmsg, e); } } diff --git a/src/java/com/threerings/presents/io/ObjectStreamException.java b/src/java/com/threerings/presents/io/ObjectStreamException.java index 5f1d5df21..755ceca55 100644 --- a/src/java/com/threerings/presents/io/ObjectStreamException.java +++ b/src/java/com/threerings/presents/io/ObjectStreamException.java @@ -1,9 +1,14 @@ // -// $Id: ObjectStreamException.java,v 1.4 2001/10/11 04:07:52 mdb Exp $ +// $Id: ObjectStreamException.java,v 1.5 2001/10/16 16:44:20 mdb Exp $ package com.threerings.presents.io; import java.io.IOException; +import java.io.PrintStream; +import java.io.PrintWriter; + +import org.apache.commons.util.exception.Nestable; +import org.apache.commons.util.exception.NestableDelegate; /** * The object stream exception is used to communicate an error in @@ -12,10 +17,100 @@ import java.io.IOException; * the nature of the error in the exception message with the assumption * that the caller will want to raise holy hell in every case. */ -public class ObjectStreamException extends IOException +public class ObjectStreamException + extends IOException implements Nestable { + /** + * Constructs an object stream exception with the specified message. + */ public ObjectStreamException (String message) { super(message); } + + /** + * Constructs an object stream exception with the specified message + * and causing exception. + */ + public ObjectStreamException (String message, Throwable cause) + { + super(message); + _cause = cause; + } + + // documentation inherited + public Throwable getCause () + { + return _cause; + } + + // documentation inherited + public String getMessage () + { + StringBuffer msg = new StringBuffer(); + + // include our message if we have one + String ourMsg = super.getMessage(); + if (ourMsg != null) { + msg.append(ourMsg); + } + + // and append the message from our causing exception if we've got + // one of those + if (_cause != null) { + String causeMsg = _cause.getMessage(); + if (causeMsg != null) { + if (ourMsg != null) { + msg.append(": "); + } + msg.append(causeMsg); + } + } + + return (msg.length() > 0 ? msg.toString() : null); + } + + /** + * Prints the stack trace of this exception the the standar error + * stream. + */ + public void printStackTrace () + { + _delegate.printStackTrace(); + } + + /** + * Prints the stack trace of this exception to the specified print + * stream. + * + * @param out {@link PrintStream} to use for output. + */ + public void printStackTrace (PrintStream out) + { + _delegate.printStackTrace(out); + } + + // documentation inherited + public void printStackTrace (PrintWriter out) + { + _delegate.printStackTrace(out); + } + + // documentation inherited + public final void printPartialStackTrace (PrintWriter out) + { + super.printStackTrace(out); + } + + /** + * The helper instance which contains much of the code which we + * delegate to. + */ + protected NestableDelegate _delegate = new NestableDelegate(this); + + /** + * Holds the reference to the exception or error that caused this + * exception to be thrown. + */ + protected Throwable _cause = null; } diff --git a/src/java/com/threerings/presents/io/TypedObjectFactory.java b/src/java/com/threerings/presents/io/TypedObjectFactory.java index 1a819e2d0..92124b279 100644 --- a/src/java/com/threerings/presents/io/TypedObjectFactory.java +++ b/src/java/com/threerings/presents/io/TypedObjectFactory.java @@ -1,5 +1,5 @@ // -// $Id: TypedObjectFactory.java,v 1.7 2001/10/11 04:07:52 mdb Exp $ +// $Id: TypedObjectFactory.java,v 1.8 2001/10/16 16:44:20 mdb Exp $ package com.threerings.presents.io; @@ -86,13 +86,9 @@ public class TypedObjectFactory try { return (TypedObject)clazz.newInstance(); } catch (Throwable t) { - Log.warning("Typed object error: " + t); - Log.logStackTrace(t); - String errmsg = "Unable to instantiate typed object " + - "[class=" + clazz.getName() + - ", error=" + t.getMessage() + "]."; - throw new ObjectStreamException(errmsg); + "[class=" + clazz.getName() + "]"; + throw new ObjectStreamException(errmsg, t); } }