GraphQLSwift / GraphQL

The Swift GraphQL implementation for macOS and Linux
MIT License
938 stars 72 forks source link

Have some Protocol for errors to allow the error to provide extra information? #147

Closed Dracks closed 1 month ago

Dracks commented 2 months ago

Hello,

I was trying my app that I'm migrating to swift, and with some wrong data it raised an error, but I've got a problem, The only information graphql was providing was that the error was App.Exception.

After looking an investigating, I've found what was the problem, I've got a custom ErrorClass that contains an error code, and some information of the error, with also some context of the error, But when It's throw in graphiti ends in the function locatedError (https://github.com/GraphQLSwift/GraphQL/blob/main/Sources/GraphQL/Error/LocatedError.swift) and this simply extracts the type of the function.

Will it be possible to have some protocol like:

protocol GraphQLErrorSummary {
   getGqlSummary() -> String
}

That can be used in case the error is acceptance of this protocol, and use that as the error description? (One of the things that I've got in the error, is the file and line in which the error was thrown.

If you wish I can try to provide a P/R.

Thanks, Dracks

NeedleInAJayStack commented 1 month ago

In the function linked, the error contents are populated using String(describing: originalError), not just the error type. As such, you can adjust what is included by conforming your error class to CustomStringConvertible. See an example here: https://developer.apple.com/documentation/swift/string/init(describing:)-67ncf

Does that work to resolve your issue?