@@ -5,7 +5,7 @@ import java.util.concurrent.{SynchronousQueue, ThreadPoolExecutor, TimeUnit}
55import com .codacy .client .bitbucket .util .HTTPStatusCodes
66import com .ning .http .client .AsyncHttpClientConfig
77import play .api .http .{ContentTypeOf , Writeable }
8- import play .api .libs .json .{ JsValue , Json , Reads }
8+ import play .api .libs .json ._
99import play .api .libs .oauth ._
1010import play .api .libs .ws .ning .{NingAsyncHttpClientConfigBuilder , NingWSClient }
1111
@@ -67,7 +67,19 @@ class BitbucketClient(key: String, secretKey: String, token: String, secretToken
6767 val jsValue = parseJson(body)
6868 jsValue match {
6969 case Right (json) =>
70- json.validate[T ].fold(_ => FailedResponse (s " Failed to parse json: $json" ), a => SuccessfulResponse (a))
70+ json.validate[T ] match {
71+ case s : JsSuccess [T ] =>
72+ SuccessfulResponse (s.value)
73+ case e : JsError =>
74+ val msg =
75+ s """
76+ |Failed to validate json:
77+ | $json
78+ |JsError errors:
79+ | ${e.errors.mkString(System .lineSeparator)}
80+ """ .stripMargin
81+ FailedResponse (msg)
82+ }
7183 case Left (message) =>
7284 FailedResponse (message.detail)
7385 }
@@ -129,14 +141,20 @@ class BitbucketClient(key: String, secretKey: String, token: String, secretToken
129141 }
130142
131143 private def parseJson (input : String ): Either [ResponseError , JsValue ] = {
132- val json = Json .parse(input)
133-
134- val errorOpt = (json \ " error" ).asOpt[ResponseError ]
135-
136- errorOpt.map {
137- error =>
138- Left (error)
139- }.getOrElse(Right (json))
144+ Try {
145+ val json = Json .parse(input)
146+ val errorOpt = (json \ " error" ).asOpt[ResponseError ]
147+
148+ errorOpt.map {
149+ error =>
150+ Left (error)
151+ }.getOrElse(Right (json))
152+ } match {
153+ case Success (jsValue) =>
154+ jsValue
155+ case Failure (e) =>
156+ Left (ResponseError (" Failed to parse json" ,e.getStackTrace.mkString(System .lineSeparator),e.getMessage))
157+ }
140158 }
141159
142160 private def withClientEither [T ](block : NingWSClient => Either [ResponseError , T ]): Either [ResponseError , T ] = {
0 commit comments