Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait CraterException extends Exception {
- val message: String
- val cause : Throwable
- }
- object CraterException {
- def catchAndTransformSqlException(message: String) = catching(classOf[SQLException]).withApply( e =>
- e match {
- case e: SQLException => throw apply(message, e)
- })
- def apply(message: String, e: SQLException): CraterException = {
- // http://www.postgresql.org/docs/8.4/interactive/errcodes-appendix.html
- e.getSQLState match {
- //UNIQUE-VOIOLATION
- case "23505" => throw UniqueConstraintViolationException(message, e)
- case _ => throw e
- }
- }
- }
- override def create(newUser: User): User = {
- CraterException.catchAndTransformSqlException("Error during user create.") {
- database withSession {
- implicit session =>
- {
- val newUserId = (users returning users.map(_.id)) += newUser
- val user = for {
- u <- users if u.id === newUserId
- } yield u
- user.first
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement