Skip to content

Commit

Permalink
fix(error): Correctly encode the error message. (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpdaniels authored and ghostdogpr committed Jan 3, 2020
1 parent bcfccab commit e71fc6c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
11 changes: 6 additions & 5 deletions core/src/main/scala/caliban/GraphQLResponse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ private object GraphQLResponceCirce {
import io.circe._
import io.circe.syntax._
val graphQLResponseEncoder: Encoder[GraphQLResponse[CalibanError]] = Encoder
.instance[GraphQLResponse[CalibanError]](
response =>
.instance[GraphQLResponse[CalibanError]] {
case GraphQLResponse(data, Nil) => Json.obj("data" -> data.asJson)
case GraphQLResponse(data, errors) =>
Json.obj(
"data" -> response.data.asJson,
"errors" -> Json.fromValues(response.errors.map(err => Json.fromString(err.toString)))
"data" -> data.asJson,
"errors" -> Json.fromValues(errors.map(err => Json.obj("message" -> Json.fromString(err.toString))))
)
)
}
}
16 changes: 15 additions & 1 deletion core/src/test/scala/caliban/GraphQLResponseSpec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package caliban

import caliban.CalibanError.ExecutionError
import caliban.Value._
import io.circe._
import io.circe.syntax._
Expand All @@ -13,7 +14,20 @@ object GraphQLResponseSpec
val response = GraphQLResponse(StringValue("data"), Nil)
assert(
response.asJson,
equalTo(Json.obj("data" -> Json.fromString("data"), "errors" -> Json.arr()))
equalTo(Json.obj("data" -> Json.fromString("data")))
)
},
test("should include error only if non-empty") {
val response = GraphQLResponse(StringValue("data"), List(ExecutionError("Resolution failed")))

assert(
response.asJson,
equalTo(
Json.obj(
"data" -> Json.fromString("data"),
"errors" -> Json.arr(Json.obj("message" -> Json.fromString("Execution error: Resolution failed")))
)
)
)
}
)
Expand Down

0 comments on commit e71fc6c

Please sign in to comment.