Advertisement
Guest User

Untitled

a guest
Jul 6th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.05 KB | None | 0 0
  1.  
  2. trait CraterException extends Exception {
  3.   val message: String
  4.   val cause : Throwable
  5. }
  6.  
  7. object CraterException {
  8.  
  9.   def catchAndTransformSqlException(message: String) = catching(classOf[SQLException]).withApply( e =>
  10.     e match {
  11.       case e: SQLException => throw apply(message, e)
  12.     })
  13.  
  14.   def apply(message: String, e: SQLException): CraterException = {
  15.     // http://www.postgresql.org/docs/8.4/interactive/errcodes-appendix.html
  16.     e.getSQLState match {
  17.       //UNIQUE-VOIOLATION
  18.       case "23505" => throw UniqueConstraintViolationException(message, e)
  19.       case _ => throw e
  20.     }
  21.   }
  22.  
  23. }
  24.  
  25.  
  26.  
  27. override def create(newUser: User): User = {
  28.     CraterException.catchAndTransformSqlException("Error during user create.") {
  29.       database withSession {
  30.         implicit session =>
  31.           {
  32.             val newUserId = (users returning users.map(_.id)) += newUser
  33.             val user = for {
  34.               u <- users if u.id === newUserId
  35.             } yield u
  36.  
  37.             user.first
  38.           }
  39.       }
  40.     }
  41.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement