@Override
 public void writeTo(StreamOutput out) throws IOException {
   out.writeOptionalString(this.getMessage());
   out.writeException(this.getCause());
   writeStackTraces(this, out);
   out.writeMapOfLists(headers, StreamOutput::writeString, StreamOutput::writeString);
 }
 /**
  * Serializes the given exceptions stacktrace elements as well as it's suppressed exceptions to
  * the given output stream.
  */
 public static <T extends Throwable> T writeStackTraces(T throwable, StreamOutput out)
     throws IOException {
   StackTraceElement[] stackTrace = throwable.getStackTrace();
   out.writeVInt(stackTrace.length);
   for (StackTraceElement element : stackTrace) {
     out.writeString(element.getClassName());
     out.writeOptionalString(element.getFileName());
     out.writeString(element.getMethodName());
     out.writeVInt(element.getLineNumber());
   }
   Throwable[] suppressed = throwable.getSuppressed();
   out.writeVInt(suppressed.length);
   for (Throwable t : suppressed) {
     out.writeException(t);
   }
   return throwable;
 }