Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Following GraphQLError extension to add error code #72 on Graphiti.
I was looking for a better way to handle GraphQL errors on client.
Inspired by Apollo Server error extensions I was looking for something similar for my Vapor server.
Swift
Error
protocolMost of the times no additional data needed to analyze an error or a thrown exception but the data already exist in a Swift custom error type.
Vapor's
Abort
andAbortError
for example already have status code, reason, stack trace and even headers.Problem 1
Accessing custom error data without casting it to its actual type is impossible.
Problem 2
Even if I can access this data, saving this
Any
as aCodable
representation will require using some king of meta type or type erasure.Codable+Extensions -
AnyCodable
Inspired by Flight-School/AnyCodable I've crated
AnyCodable
class, along side the existingAnyEncodable
, that can wrap Any Codable type, and encode\decode it ad a dictionary value.Error+Extensions
Using Swift's Mirror I was able to create a dictionary representation reflecting all of the error's additional data, that otherwise was hidden to the
GraphQL
module.I've added a new Swift Error extension to easily create reflection for a given error.
GraphQLError
Next step was to add
extensions
property to theGraphQLError
struct.The result
Now error description response contains a new
extensions
field that reflect any additional data held by the original error.TODOs and other considerations
AnyCodable
class does not represent nil values as for now, any nil decoded value will be presented as "Null" stringAnyCodable
supported types can be found on theinit(from:)
initializer. any new type that we want to support in the future will have to be added manually.