<commit_before>package com.tribbloids.spookystuff.utils

import com.tribbloids.spookystuff.utils.MultiCausesException.TreeNodeView
import org.apache.spark.sql.catalyst.trees.TreeNode

import scala.util.{Failure, Try}

object MultiCausesException {

  case class TreeNodeView(self: Throwable) extends TreeNode[TreeNodeView] {
    override def children: Seq[TreeNodeView] = {
      self match {
        case v: MultiCausesException =>
          v.causes.map(TreeNodeView)
        case _ =>
          val eOpt = Option(self).flatMap(
            v =>
              Option(v.getCause)
          )
          eOpt.map(TreeNodeView).toSeq
      }
    }

    override def simpleString(): String = {
      self match {
        case v: MultiCausesException =>
          v.simpleMessage
        case _ =>
          self.getClass.getName + ": " + self.getMessage
      }
    }
  }

  def &&&[T](trials: Seq[Try[T]], agg: Seq[Throwable] => MultiCausesException = WithCauses): Seq[T] = {
    val es = trials.collect{
      case Failure(e) => e
    }
    if (es.isEmpty) {
      trials.map(_.get)
    }
    else {
      throw agg(es)
    }
  }
}

trait MultiCausesException extends Throwable {

  def causes: Seq[Throwable] = Nil

  lazy val treeNodeView = TreeNodeView(this)

  override def getMessage: String = treeNodeView.toString()

  def simpleMessage: String = "[MULTIPLE CAUSES]"
}

case class WithCauses(override val causes: Seq[Throwable] = Nil) extends MultiCausesException<commit_msg>MultiCausesException now always use the first child as cause<commit_after>package com.tribbloids.spookystuff.utils

import com.tribbloids.spookystuff.utils.MultiCausesException.TreeNodeView
import org.apache.spark.sql.catalyst.trees.TreeNode

import scala.util.{Failure, Try}

object MultiCausesException {

  case class TreeNodeView(self: Throwable) extends TreeNode[TreeNodeView] {
    override def children: Seq[TreeNodeView] = {
      self match {
        case v: MultiCausesException =>
          v.causes.map(TreeNodeView)
        case _ =>
          val eOpt = Option(self).flatMap(
            v =>
              Option(v.getCause)
          )
          eOpt.map(TreeNodeView).toSeq
      }
    }

    override def simpleString(): String = {
      self match {
        case v: MultiCausesException =>
          v.simpleMessage
        case _ =>
          self.getClass.getName + ": " + self.getMessage
      }
    }
  }

  def &&&[T](trials: Seq[Try[T]], agg: Seq[Throwable] => MultiCausesException = WithCauses): Seq[T] = {
    val es = trials.collect{
      case Failure(e) => e
    }
    if (es.isEmpty) {
      trials.map(_.get)
    }
    else {
      throw agg(es)
    }
  }
}

trait MultiCausesException extends Throwable {

  def causes: Seq[Throwable] = Nil

  lazy val treeNodeView = TreeNodeView(this)

  override def getMessage: String = treeNodeView.toString()

  override def getCause: Throwable = causes.headOption.orNull

  def simpleMessage: String = "[MULTIPLE CAUSES]"
}

case class WithCauses(override val causes: Seq[Throwable] = Nil) extends MultiCausesException
{'fraction_non_alphanumeric': 0.103515625, 'fraction_numerical': 0.0, 'mean_word_length': 2.729368932038835, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5046969', 'n_tokens_mistral': 991, 'n_tokens_neox': 964, 'n_words': 208}