Skip to content

Improve error handling to allow users to make a decision on the type of error #331

@soilidokay

Description

@soilidokay

I want to handle some of my own exceptions, but I can't do it yet, it seems like the system's try-catch feature is preventing it.
I found it in the source code

 private async Task<QueryResult> DoExecuteRequestAsync(QueryRequest gql, TContextType? overwriteContext, IServiceProvider? serviceProvider, ClaimsPrincipal? user, ExecutionOptions? options)
 {
     try
     {
         if (options == null)
         {
             options = new ExecutionOptions();
         }

         GraphQLDocument graphQLDocument = null;
         if (options!.EnablePersistedQueries)
         {
             PersistedQueryExtension persistedQueryExtension = (PersistedQueryExtension)ExpressionUtil.ChangeType(gql.Extensions.GetValueOrDefault("persistedQuery"), typeof(PersistedQueryExtension), null);
             if (persistedQueryExtension != null && persistedQueryExtension.Version != 1)
             {
                 throw new EntityGraphQLExecutionException("PersistedQueryNotSupported");
             }

             string text = persistedQueryExtension?.Sha256Hash;
             if (text == null && gql.Query == null)
             {
                 throw new EntityGraphQLExecutionException("Please provide a persisted query hash or a query string");
             }

             if (text != null)
             {
                 graphQLDocument = queryCache.GetCompiledQueryWithHash(text);
                 if (graphQLDocument == null && gql.Query == null)
                 {
                     throw new EntityGraphQLExecutionException("PersistedQueryNotFound");
                 }

                 if (graphQLDocument == null)
                 {
                     graphQLDocument = graphQLCompiler.Compile(gql, new QueryRequestContext(AuthorizationService, user));
                     queryCache.AddCompiledQuery(text, graphQLDocument);
                 }
             }
             else if (graphQLDocument == null)
             {
                 graphQLDocument = ((!options!.EnableQueryCache) ? graphQLCompiler.Compile(gql, new QueryRequestContext(AuthorizationService, user)) : CompileQueryWithCache(gql, user));
             }
         }
         else if (options!.EnableQueryCache)
         {
             graphQLDocument = CompileQueryWithCache(gql, user);
         }
         else
         {
             if (gql.Query == null)
             {
                 if (((PersistedQueryExtension)ExpressionUtil.ChangeType(gql.Extensions.GetValueOrDefault("persistedQuery"), typeof(PersistedQueryExtension), null))?.Sha256Hash != null)
                 {
                     throw new EntityGraphQLExecutionException("PersistedQueryNotSupported");
                 }

                 throw new ArgumentNullException("Query", "Query must be set unless you are using persisted queries");
             }

             graphQLDocument = graphQLCompiler.Compile(gql, new QueryRequestContext(AuthorizationService, user));
         }

         return await graphQLDocument.ExecuteQueryAsync(overwriteContext, serviceProvider, gql.Variables, gql.OperationName, options);
     }
     catch (Exception exception)
     {
         return HandleException(exception);
     }
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions