<commit_before>package unfiltered.netty.cycle

import org.jboss.netty.handler.codec.http.{
  HttpRequest=>NHttpRequest,HttpResponse=>NHttpResponse,HttpChunk=>NHttpChunk}
import org.jboss.netty.channel._
import org.jboss.netty.handler.codec.http.HttpResponseStatus._
import org.jboss.netty.handler.codec.http.HttpVersion._
import unfiltered.netty._
import unfiltered.response.{ResponseFunction, Pass}
import unfiltered.request.HttpRequest

object Plan {
  type Intent = unfiltered.Cycle.Intent[ReceivedMessage,NHttpResponse]
}
/** Object to facilitate Plan.Intent definitions. Type annotations
 *  are another option. */
object Intent {
  def apply(intent: Plan.Intent) = intent
}
/** A Netty Plan for request cycle handling. */
trait Plan extends SimpleChannelUpstreamHandler with ExceptionHandler {
  def intent: Plan.Intent
  def catching(ctx: ChannelHandlerContext)(thunk: => Unit) {
    try { thunk } catch {
      case e => onException(ctx, e)
    }
  }

  private lazy val guardedIntent = intent.fold(
    (req: HttpRequest[ReceivedMessage]) =>
      req.underlying.context.sendUpstream(req.underlying.event),
    (req: HttpRequest[ReceivedMessage],
     rf: ResponseFunction[NHttpResponse]) =>
      executeResponse {
        catching(req.underlying.context) {
          req.underlying.respond(rf)
        }
      }
  )
  override def messageReceived(ctx: ChannelHandlerContext,
                               e: MessageEvent) {
    e.getMessage() match {
      case req:NHttpRequest =>
        catching(ctx) {
          executeIntent { guardedIntent(
            new RequestBinding(ReceivedMessage(req, ctx, e))
          ) }
        }
      case chunk:NHttpChunk => ctx.sendUpstream(e)
      case msg => error("Unexpected message type from upstream: %s"
                        .format(msg))
    }
  }
  def executeIntent(thunk: => Unit)
  def executeResponse(thunk: => Unit)
  def shutdown()
}

class Planify(val intent: Plan.Intent)
extends Plan with ThreadPool with ServerErrorResponse

object Planify {
  def apply(intent: Plan.Intent) = new Planify(intent)
}<commit_msg>catch exceptions thrown by guardedIntent<commit_after>package unfiltered.netty.cycle

import org.jboss.netty.handler.codec.http.{
  HttpRequest=>NHttpRequest,HttpResponse=>NHttpResponse,HttpChunk=>NHttpChunk}
import org.jboss.netty.channel._
import org.jboss.netty.handler.codec.http.HttpResponseStatus._
import org.jboss.netty.handler.codec.http.HttpVersion._
import unfiltered.netty._
import unfiltered.response.{ResponseFunction, Pass}
import unfiltered.request.HttpRequest

object Plan {
  type Intent = unfiltered.Cycle.Intent[ReceivedMessage,NHttpResponse]
}
/** Object to facilitate Plan.Intent definitions. Type annotations
 *  are another option. */
object Intent {
  def apply(intent: Plan.Intent) = intent
}
/** A Netty Plan for request cycle handling. */
trait Plan extends SimpleChannelUpstreamHandler with ExceptionHandler {
  def intent: Plan.Intent
  def catching(ctx: ChannelHandlerContext)(thunk: => Unit) {
    try { thunk } catch {
      case e => onException(ctx, e)
    }
  }

  private lazy val guardedIntent = intent.fold(
    (req: HttpRequest[ReceivedMessage]) =>
      req.underlying.context.sendUpstream(req.underlying.event),
    (req: HttpRequest[ReceivedMessage],
     rf: ResponseFunction[NHttpResponse]) =>
      executeResponse {
        catching(req.underlying.context) {
          req.underlying.respond(rf)
        }
      }
  )
  override def messageReceived(ctx: ChannelHandlerContext,
                               e: MessageEvent) {
    e.getMessage() match {
      case req:NHttpRequest =>
        catching(ctx) {
          executeIntent {
            catching(ctx) {
              guardedIntent(
                new RequestBinding(ReceivedMessage(req, ctx, e))
              )
            }
          }
        }
      case chunk:NHttpChunk => ctx.sendUpstream(e)
      case msg => error("Unexpected message type from upstream: %s"
                        .format(msg))
    }
  }
  def executeIntent(thunk: => Unit)
  def executeResponse(thunk: => Unit)
  def shutdown()
}

class Planify(val intent: Plan.Intent)
extends Plan with ThreadPool with ServerErrorResponse

object Planify {
  def apply(intent: Plan.Intent) = new Planify(intent)
}
{'fraction_non_alphanumeric': 0.10119465917076599, 'fraction_numerical': 0.0, 'mean_word_length': 3.219367588932806, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2123681', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1206, 'n_words': 275}