Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(error): Correctly encode the error message. #145

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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