-
Notifications
You must be signed in to change notification settings - Fork 202
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
feat: [API] Merge non-GraphQL spec error fields into GraphQLError.extensions #401
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import Amplify | ||
|
||
extension GraphQLResponseDecoder { | ||
|
||
static func decodeErrors(graphQLErrors: [JSONValue]) throws -> [GraphQLError] { | ||
var responseErrors = [GraphQLError]() | ||
for error in graphQLErrors { | ||
do { | ||
let responseError = try decode(graphQLErrorJSON: error) | ||
responseErrors.append(responseError) | ||
} catch let decodingError as DecodingError { | ||
throw APIError(error: decodingError) | ||
} catch { | ||
throw APIError.unknown(""" | ||
Unexpected failure while decoding GraphQL response containing errors: | ||
\(String(describing: graphQLErrors)) | ||
""", "", error) | ||
} | ||
} | ||
|
||
return responseErrors | ||
} | ||
|
||
static func decode(graphQLErrorJSON: JSONValue) throws -> GraphQLError { | ||
let serializedJSON = try JSONEncoder().encode(graphQLErrorJSON) | ||
let decoder = JSONDecoder() | ||
decoder.dateDecodingStrategy = ModelDateFormatting.decodingStrategy | ||
let graphQLError = try decoder.decode(GraphQLError.self, from: serializedJSON) | ||
return mergeExtensions(from: graphQLErrorJSON, graphQLError: graphQLError) | ||
} | ||
|
||
/// Merge fields which are not in the generic GraphQL error json over into the `GraphQLError.extensions` | ||
/// This is the opinionated implementation of the plugin to store service errors which do not conform to the | ||
/// GraphQL Error spec (https://spec.graphql.org/June2018/#sec-Errors) | ||
private static func mergeExtensions(from graphQLErrorJSON: JSONValue, graphQLError: GraphQLError) -> GraphQLError { | ||
|
||
var mergedExtensions = [String: JSONValue]() | ||
if let graphQLErrorExtensions = graphQLError.extensions { | ||
mergedExtensions = graphQLErrorExtensions | ||
} | ||
|
||
guard case let .object(graphQLErrorObject) = graphQLErrorJSON else { | ||
return graphQLError | ||
} | ||
|
||
graphQLErrorObject.forEach { key, value in | ||
if key == "message" || | ||
key == "locations" || | ||
key == "path" || | ||
key == "extensions" || | ||
mergedExtensions.keys.contains(key) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could have a list of keys and then append the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, updating |
||
return | ||
} | ||
|
||
mergedExtensions[key] = value | ||
} | ||
|
||
return GraphQLError(message: graphQLError.message, | ||
locations: graphQLError.locations, | ||
path: graphQLError.path, | ||
extensions: !mergedExtensions.isEmpty ? mergedExtensions : nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should that second argument contain something? Does the "report to AWS" suggestion fit here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks like we already do that when exposed to the developer