Skip to content

Commit

Permalink
Fix edge case where error message is null (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnawf authored Aug 4, 2023
1 parent 6e10a07 commit 73a449f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/src/main/java/graphql/nadel/engine/util/GraphQLUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fun toGraphQLError(
raw: JsonMap,
): GraphQLError {
val errorBuilder = newError()
.message(raw["message"] as String?)
.message((raw["message"] as String?) ?: "An error has occurred")
raw["extensions"]?.let { extensions ->
errorBuilder.extensions(extensions as JsonMap)
}
Expand Down
71 changes: 61 additions & 10 deletions lib/src/test/kotlin/graphql/nadel/engine/util/GraphQLUtilKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,110 @@ class GraphQLUtilKtTest : DescribeSpec({
.build()

describe("getFieldAt tests") {

it("can find fields at single path") {

val fieldDef = aType.getFieldAt(listOf("f1"))

assert(fieldDef?.name == "f1")
assert(fieldDef?.type == bType)
}
it("can find fields at multi path") {

it("can find fields at multi path") {
val fieldDef = aType.getFieldAt(listOf("f1", "f2"))

assert(fieldDef?.name == "f2")
assert(fieldDef?.type == GraphQLString)
}
it("will return null with bogus paths") {

it("will return null with bogus paths") {
val fieldDef = aType.getFieldAt(listOf("f1", "fX"))

assert(fieldDef == null)
}
it("will return null with bogus paths that go over non containers") {

it("will return null with bogus paths that go over non containers") {
val fieldDef = aType.getFieldAt(listOf("f1", "f2", "fX"))

assert(fieldDef == null)
}
}
describe("getFieldContainerAt tests") {

describe("getFieldContainerAt tests") {
it("can find container at single path") {

val fieldContainer = aType.getFieldContainerAt(listOf("f1"))

assert(fieldContainer?.name == "A")
}
it("can find field containers at multi path") {

it("can find field containers at multi path") {
val fieldContainer = aType.getFieldContainerAt(listOf("f1", "f2"))

assert(fieldContainer?.name == "B")
}
it("will return null with bogus paths") {

it("will return null with bogus paths") {
val fieldContainer = aType.getFieldContainerAt(listOf("f1", "fX"))

assert(fieldContainer == null)
}
it("will return null with bogus paths that go over non containers") {

it("will return null with bogus paths that go over non containers") {
val fieldContainer = aType.getFieldContainerAt(listOf("f1", "f2", "fX"))

assert(fieldContainer == null)
}
}

describe("toGraphQLError") {
it("handles case where error message is null") {
val map = mapOf("message" to null)

// When
val error = toGraphQLError(map)

// Then
assert(error.message == "An error has occurred")
}

it("handles case where error message is missing") {
val map: JsonMap = mapOf()

// When
val error = toGraphQLError(map)

// Then
assert(error.message == "An error has occurred")
}

it("copies error message") {
val map = mapOf("message" to "banana")

// When
val error = toGraphQLError(map)

// Then
assert(error.message == "banana")
}

it("copies extensions") {
val map = mapOf("extensions" to mapOf("hello" to "world"))

// When
val error = toGraphQLError(map)

// Then
assert(error.message == "An error has occurred")
assert(error.extensions["hello"] == "world")
}

it("copies path") {
val map = mapOf("path" to listOf("hello", "world"))

// When
val error = toGraphQLError(map)

// Then
assert(error.message == "An error has occurred")
assert(error.path == listOf("hello", "world"))
}
}
})

0 comments on commit 73a449f

Please sign in to comment.