@@ -5,7 +5,7 @@ import java.util.concurrent.{SynchronousQueue, ThreadPoolExecutor, TimeUnit}
5
5
import com .codacy .client .bitbucket .util .HTTPStatusCodes
6
6
import com .ning .http .client .AsyncHttpClientConfig
7
7
import play .api .http .{ContentTypeOf , Writeable }
8
- import play .api .libs .json .{ JsValue , Json , Reads }
8
+ import play .api .libs .json ._
9
9
import play .api .libs .oauth ._
10
10
import play .api .libs .ws .ning .{NingAsyncHttpClientConfigBuilder , NingWSClient }
11
11
@@ -67,7 +67,19 @@ class BitbucketClient(key: String, secretKey: String, token: String, secretToken
67
67
val jsValue = parseJson(body)
68
68
jsValue match {
69
69
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
+ }
71
83
case Left (message) =>
72
84
FailedResponse (message.detail)
73
85
}
@@ -129,14 +141,20 @@ class BitbucketClient(key: String, secretKey: String, token: String, secretToken
129
141
}
130
142
131
143
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
+ }
140
158
}
141
159
142
160
private def withClientEither [T ](block : NingWSClient => Either [ResponseError , T ]): Either [ResponseError , T ] = {
0 commit comments