-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor MaybeThrowGraphqlException to throw AggregateException with …
…all GraphQL errors (#169) * first pass * Updated tests * Removed path from message
- Loading branch information
Showing
20 changed files
with
451 additions
and
412 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
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,49 @@ | ||
using System.Diagnostics.Contracts; | ||
using GraphQL; | ||
|
||
namespace Speckle.Sdk.Api.GraphQL; | ||
|
||
internal static class GraphQLErrorHandler | ||
{ | ||
/// <exception cref="AggregateException"><inheritdoc cref="EnsureGraphQLSuccess(IReadOnlyCollection{GraphQLError}?)"/></exception> | ||
public static void EnsureGraphQLSuccess(this IGraphQLResponse response) => EnsureGraphQLSuccess(response.Errors); | ||
|
||
/// <exception cref="AggregateException">Containing a <see cref="SpeckleGraphQLException"/> (or subclass of) for each graphql Error</exception> | ||
public static void EnsureGraphQLSuccess(IReadOnlyCollection<GraphQLError>? errors) | ||
{ | ||
// The errors reflect the Apollo server v2 API, which is deprecated. It is bound to change, | ||
// once we migrate to a newer version. | ||
if (errors == null || errors.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
List<SpeckleGraphQLException> exceptions = new(errors.Count); | ||
foreach (var error in errors) | ||
{ | ||
object? code = null; | ||
_ = error.Extensions?.TryGetValue("code", out code); | ||
|
||
var message = FormatErrorMessage(error, code); | ||
var ex = code switch | ||
{ | ||
"GRAPHQL_PARSE_FAILED" or "GRAPHQL_VALIDATION_FAILED" => new SpeckleGraphQLInvalidQueryException(message), | ||
"FORBIDDEN" or "UNAUTHENTICATED" => new SpeckleGraphQLForbiddenException(message), | ||
"STREAM_NOT_FOUND" => new SpeckleGraphQLStreamNotFoundException(message), | ||
"BAD_USER_INPUT" => new SpeckleGraphQLBadInputException(message), | ||
"INTERNAL_SERVER_ERROR" => new SpeckleGraphQLInternalErrorException(message), | ||
_ => new SpeckleGraphQLException(message), | ||
}; | ||
exceptions.Add(ex); | ||
} | ||
|
||
throw new AggregateException("Request failed with GraphQL errors, see inner exceptions", exceptions); | ||
} | ||
|
||
[Pure] | ||
private static string FormatErrorMessage(GraphQLError error, object? code) | ||
{ | ||
code ??= "ERROR"; | ||
return $"{code}: {error.Message}"; | ||
} | ||
} |
Oops, something went wrong.