/** * Returns a clean message for the specified throwable. * * <p>This method can be used to provide more user-friendly messages by removing the exception * class prefix to the message if the message is identical to that of the exception causing it. * * @param ex the throwable. * * @return a clean message. */ public static String cleanMessage(Throwable ex) { Throwable cause = ex.getCause(); String message = ex.getMessage(); if (message == null) { message = "No message"; } if (cause == null) return message; if (!message.equals(cause.getClass().getName()+": "+cause.getMessage())) return message; else return cleanMessage(cause); }
/** * Returns the XML for a generic exception. * * @param ex The exception to turn to XML. * @param xml The XML writer. * @param wrap Whether to wrap the XML into an element. * * @throws IOException Only if thrown by the XML writer. */ private static void asExceptionXML(Exception ex, XMLWriter xml, boolean wrap) throws IOException { if (wrap) { xml.openElement("exception"); } xml.attribute("class", ex.getClass().getName()); xml.element("message", cleanMessage(ex)); xml.element("stack-trace", Errors.getStackTrace(ex, true)); Throwable cause = ex.getCause(); if (cause != null) { xml.openElement("cause"); toXML(cause, xml, false); xml.closeElement(); } if (wrap) { xml.closeElement(); } }
/** * Returns the XML for a generic error. * * @param ex The error to turn to XML. * @param xml The XML writer. * @param wrap Whether to wrap the XML into an element. * * @throws IOException Only if thrown by the XML writer. */ private static void asErrorXML(Error ex, XMLWriter xml, boolean wrap) throws IOException { if (wrap) { xml.openElement("error"); } xml.attribute("class", ex.getClass().getName()); xml.element("message", cleanMessage(ex)); xml.element("stack-trace", Errors.getStackTrace(ex, true)); Throwable cause = ex.getCause(); if (cause != null) { xml.openElement("cause"); toXML(cause, xml, false); xml.closeElement(); } if (wrap) { xml.closeElement(); } }
xml.closeElement(); xml.element("title", toTitle(id)); xml.element("message", Errors.cleanMessage(ex));