+ private Output createErrorOutput(Reaction reaction, State state) {
+ DefaultOutput output = new DefaultOutput(String.format("Error while processing “%s!”", reaction.name()));
+ output.addText("text/plain", createErrorEmailText(reaction, state));
+ output.addText("text/html", createErrorEmailText(reaction, state));
+ return output;
+ }
+
+ private String createErrorEmailText(Reaction reaction, State state) {
+ StringBuilder emailText = new StringBuilder();
+ emailText.append(String.format("An error occured while processing “%s.”\n\n", reaction.name()));
+ appendExceptionToEmailText(state.exception(), emailText);
+ return emailText.toString();
+ }
+
+ private void appendExceptionToEmailText(Throwable exception, StringBuilder emailText) {
+ if (exception != null) {
+ try (StringWriter stringWriter = new StringWriter();
+ PrintWriter printWriter = new PrintWriter(stringWriter)) {
+ exception.printStackTrace(printWriter);
+ emailText.append(stringWriter.toString());
+ } catch (IOException ioe1) {
+ /* StringWriter doesn’t throw. */
+ throw new RuntimeException(ioe1);
+ }
+ }
+ }
+