Skip to content

Commit

Permalink
Use Try
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydmeta committed Feb 20, 2015
1 parent 98edbff commit a1b2d10
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions enumeratum-play/src/main/scala/enumeratum/Json.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package enumeratum

import play.api.libs.json._

import scala.util.Try
import scala.util.control.NonFatal

/**
Expand All @@ -15,10 +16,10 @@ object Json {
def reads[A](enum: Enum[A]): Reads[A] = new Reads[A] {
def reads(json: JsValue): JsResult[A] = json match {
case JsString(s) => {
try {
Try {
JsSuccess(enum.withName(s))
} catch {
case NonFatal(e) => JsError(s"Enumeration expected of type: '$enum', but it does not appear to contain the value: '$s'")
} getOrElse {
JsError(s"Enumeration expected of type: '$enum', but it does not appear to contain the value: '$s'")
}
}
case _ => JsError("String value expected")
Expand Down

0 comments on commit a1b2d10

Please sign in to comment.