-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [API] Merge non-GraphQL spec error fields into GraphQLError.ext…
…ensions (#401) * feat: [API] Merge non-GraphQL spec error fields into GraphQLError.extensions * add AppSyncErrorType * clean up mergeExtensions
- Loading branch information
Showing
11 changed files
with
383 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...fyPlugins/API/AWSAPICategoryPlugin/Support/Utils/GraphQLResponseDecoder+DecodeError.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// 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 keys = ["message", "locations", "path", "extensions"] | ||
var mergedExtensions = [String: JSONValue]() | ||
if let graphQLErrorExtensions = graphQLError.extensions { | ||
mergedExtensions = graphQLErrorExtensions | ||
keys += mergedExtensions.keys | ||
} | ||
|
||
guard case let .object(graphQLErrorObject) = graphQLErrorJSON else { | ||
return graphQLError | ||
} | ||
|
||
graphQLErrorObject.forEach { key, value in | ||
if keys.contains(key) { | ||
return | ||
} | ||
|
||
mergedExtensions[key] = value | ||
} | ||
|
||
return GraphQLError(message: graphQLError.message, | ||
locations: graphQLError.locations, | ||
path: graphQLError.path, | ||
extensions: mergedExtensions.isEmpty ? nil : mergedExtensions) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.