https://kotlinlang.org logo
Title
m

Marco Pierucci

07/18/2022, 2:11 PM
Hello! Whats the approach for handling validation erros in apollo kotlin? I used to catch refresh token issues in
customAttributes
Now Im trying to update to apollo 3.4 and switched to
nonStandardFields
as suggested. But now when getting refresh token errors,
nonStandardFields
come as null and the actual erros come as:
extensions = {errorClass=VALIDATION, validationErrors=[{message=Please make sure the refresh token is correct., inputPath=[input, input]}]}
extension being :
Map<String, Any?>?
🤔
m

mbonnin

07/18/2022, 2:12 PM
Hi 👋 that's interesting! do you have the returned json by any chance?
m

Marco Pierucci

07/18/2022, 2:13 PM
NetworkException(errors=[Error(message = Invalid data, locations = [Location(line = 1, column = 65)], path=[obtainCoconutToken], extensions = {errorClass=VALIDATION, validationErrors=[{message=Please make sure the refresh token is correct., inputPath=[input, input]}]}, nonStandardFields = null)])
would this work?
m

mbonnin

07/18/2022, 2:13 PM
That helps!
Let me check
🙌 1
Looks like you can get the errorClass with something like this:
response.errors[0].extensions["errorClass"] as? String
More recent versions of the spec now support "extensions". I guess this is why Apollo 2 didn't have support for this: https://spec.graphql.org/draft/#sel-GAPHRPfCCBCEoD2xG
m

Marco Pierucci

07/18/2022, 2:26 PM
Yeah that gives me the error class, but what about If I need to get the validationErrors message
Feels like it can become quite convoluted ( and am also tied to a string
errorClass
or
validationsError
that could easily change on an update an break my app?
m

mbonnin

07/18/2022, 2:27 PM
Then you should be able to "cast" your way down:
response.errors[0].extensions["validationErrors"] as? List<Map<String, Any?>>
(response.errors[0].extensions["validationErrors"] as? List<Map<String, Any?>>).get(0).get("message")
m

Marco Pierucci

07/18/2022, 2:28 PM
yeah exactly, woudl this be the recommended way to go?
m

mbonnin

07/18/2022, 2:28 PM
Yep.
m

Marco Pierucci

07/18/2022, 2:29 PM
https://github.com/apollographql/apollo-kotlin/issues/1964 I know its not the same but on apollo 2 and before seems like we could look for it in customAttributes so maybe it should be showing in non standardFields on 3.x and above?
Martin Bonnin [4:28 PM]
Yep.
Ah ok
well thanks then
m

mbonnin

07/18/2022, 2:29 PM
I usually define helpers like this in my projects
inline fun <reified T> Any?.cast() = this as T

val Any?.asMap: Map<String, Any?>
  get() = this.cast()
val Any?.asList: List<Any?>
  get() = this.cast()
val Any?.asString: String
  get() = this.cast()
val Any?.asBoolean: String
  get() = this.cast()
val Any?.asNumber: Number
  get() = this.cast()
This makes it easier to cast without breaking the flow
response.errors[0].extensions["validationErrors"].asList[0].asMap["message"]
I know its not the same but on apollo 2 and before seems like we could look for it in customAttributes so maybe it should be showing in non standardFields on 3.x and above?
The ways your server answers is actually standard 🙂 . It's all in "extensions" so it's all good
m

Marco Pierucci

07/18/2022, 2:31 PM
yeah it looks better I guess
cool thanks again!
m

mbonnin

07/18/2022, 2:31 PM
Sure thing!