Skip to content

Commit

Permalink
Adds CommandHandler trait and ModelCommand base class
Browse files Browse the repository at this point in the history
  • Loading branch information
casualjim committed Nov 3, 2012
1 parent e15ca70 commit ecc8bfa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.scalatra
package databinding

import scala.util.control.Exception.allCatch
import grizzled.slf4j.Logger
import validation._
import scalaz._
import Scalaz._

trait CommandHandler {
@transient private[this] lazy val logger: Logger = Logger[this.type]
def execute[S: Manifest](cmd: ModelCommand[S]): ModelValidation[S] = {
logger.debug("Executing [%s].\n%s" format (cmd.getClass.getName, cmd))
if (cmd.isValid) {
val res = (allCatch withApply (serverError(cmd.getClass.getName, _))) {
handle.lift(cmd).map(_.map(_.asInstanceOf[S])) | ValidationError("Don't know how to handle: " + cmd.getClass.getName, UnknownError).failNel
}
val ftext = "with %d failures\n%s".format(~res.fail.toOption.map(_.list.size), ~res.fail.toOption.map(_.list))
logger.debug("Command [%s] executed %s." format (cmd.getClass.getName, res.isSuccess ? "successfully." | ftext))
res
} else {
val f = cmd.errors.map(_.validation) collect {
case Failure(e) e
}
logger.debug("Command [%s] executed with %d failures.\n%s" format (cmd.getClass.getName, f.size, f.toList))
nel(f.head, f.tail: _*).fail
}
}

private[this] def serverError[R](cmdName: String, ex: Throwable): ModelValidation[R] = {
logger.error("There was an error while executing " + cmdName, ex)
ValidationError("An error occurred while handling: " + cmdName, UnknownError).failNel[R]
}

type Handler = PartialFunction[ModelCommand[_], ModelValidation[_]]

protected def handle: Handler
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.scalatra
package databinding

abstract class ModelCommand[T:Manifest] extends Command {
type ModelType = T
}
4 changes: 4 additions & 0 deletions notes/2.2.0.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@
* the models property in the `SwaggerSupport` can now be Set as `Map(classOf[Pet])`
* responseClass can now be set with a type annotation `responseClass[Pet]`

### Swagger-Ext

* Adds support for authorization to Swagger. Shows only actions a user is allowed to see.

### Testing
* Added a method `ensureSessionIsSerializable` method which will try to serialize every session value.

Expand Down

0 comments on commit ecc8bfa

Please sign in to comment.