How to use the apollo-server-errors.formatApolloErrors function in apollo-server-errors

To help you get started, we’ve selected a few apollo-server-errors examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github commercetools / merchant-center-application-kit / graphql-test-utils / create-graphql-mock-server.js View on Github external
schema,
      mocks,
    });
    const { query, variables, operationName } = JSON.parse(req.body());
    const rootValue = getRootValue(operations, operationName, variables);
    let response;
    try {
      response = await graphql({
        schema,
        source: query,
        variableValues: variables,
        rootValue,
      });
    } catch (error) {
      // https://github.com/apollographql/apollo-server/blob/61e69412c0aa88b358a2eef4ba635e8618db9946/packages/apollo-server-core/src/runHttpQuery.ts#L295-L297
      response = { error: formatApolloErrors([error]) };
    }
    return res.status(200).body(JSON.stringify(response));
  };
  const handleProxyGraphQLRequest = async (req, res) => {
github apollographql / apollo-server / packages / apollo-server-core / src / runQuery.ts View on Github external
(): Promise => {
        // Parse the document.
        let documentAST: DocumentNode;
        if (options.parsedQuery) {
          documentAST = options.parsedQuery;
        } else if (!options.queryString) {
          throw new Error('Must supply one of queryString and parsedQuery');
        } else {
          const parsingDidEnd = extensionStack.parsingDidStart({
            queryString: options.queryString,
          });
          let graphqlParseErrors: SyntaxError[] | undefined;
          try {
            documentAST = parse(options.queryString);
          } catch (syntaxError) {
            graphqlParseErrors = formatApolloErrors(
              [
                fromGraphQLError(syntaxError, {
                  errorClass: SyntaxError,
                }),
              ],
              {
                debug,
              },
            );
            return Promise.resolve({ errors: graphqlParseErrors });
          } finally {
            parsingDidEnd(...(graphqlParseErrors || []));
          }
        }

        if (
github apollographql / apollo-server / packages / apollo-server-core / src / runQuery.ts View on Github external
(): Promise => {
        // Parse the document.
        let documentAST: DocumentNode;
        if (options.parsedQuery) {
          documentAST = options.parsedQuery;
        } else if (!options.queryString) {
          throw new Error('Must supply one of queryString and parsedQuery');
        } else {
          const parsingDidEnd = extensionStack.parsingDidStart({
            queryString: options.queryString,
          });
          let graphqlParseErrors: SyntaxError[] | undefined;
          try {
            documentAST = parse(options.queryString);
          } catch (syntaxError) {
            graphqlParseErrors = formatApolloErrors(
              [
                fromGraphQLError(syntaxError, {
                  errorClass: SyntaxError,
                }),
              ],
              {
                debug,
              },
            );
            return Promise.resolve({ errors: graphqlParseErrors });
          } finally {
            parsingDidEnd(...(graphqlParseErrors || []));
          }
        }

        if (
github apollographql / apollo-server / packages / apollo-server-core / src / runHttpQuery.ts View on Github external
persistedQueryRegister,
      };

      return runQuery(params);
    } catch (e) {
      // Populate any HttpQueryError to our handler which should
      // convert it to Http Error.
      if (e.name === 'HttpQueryError') {
        // async function wraps this in a Promise
        throw e;
      }

      // This error will be uncaught, so we need to wrap it and treat it as an
      // internal server error
      return {
        errors: formatApolloErrors([e], optionsObject),
      };
    }
  }) as Array }>>;
github apollographql / apollo-server / packages / apollo-server-core / src / runHttpQuery.ts View on Github external
const defaultHeaders = { 'Content-Type': 'application/json' };
  // force no-cache on PersistedQuery errors
  const headers = hasPersistedQueryError(errors)
    ? {
        ...defaultHeaders,
        'Cache-Control': 'private, no-cache, must-revalidate',
      }
    : defaultHeaders;

  type Result =
   & Pick
   & { errors: E[] | ApolloError[] }

  const result: Result = {
    errors: options
      ? formatApolloErrors(errors, {
          debug: options.debug,
          formatter: options.formatError,
        })
      : errors,
  };

  if (extensions) {
    result.extensions = extensions;
  }

  throw new HttpQueryError(
    statusCode,
    prettyJSONStringify(result),
    true,
    headers,
  );
github apollographql / apollo-server / packages / apollo-server-core / src / ApolloServer.ts View on Github external
connection.formatResponse = (value: ExecutionResult) => ({
            ...value,
            errors:
              value.errors &&
              formatApolloErrors([...value.errors], {
                formatter: this.requestOptions.formatError,
                debug: this.requestOptions.debug,
              }),
          });
github apollographql / apollo-server / packages / apollo-server-core / src / runHttpQuery.ts View on Github external
function throwHttpGraphQLError(
  statusCode: number,
  errors: Array,
  optionsObject?: Partial,
): never {
  throw new HttpQueryError(
    statusCode,
    prettyJSONStringify({
      errors: optionsObject
        ? formatApolloErrors(errors, {
            debug: optionsObject.debug,
            formatter: optionsObject.formatError,
          })
        : errors,
    }),
    true,
    {
      'Content-Type': 'application/json',
    },
  );
}
github reactioncommerce / reaction / imports / plugins / core / core / server / startup / startNodeApp.js View on Github external
onOperation: async (message, connection) => {
        connection.formatResponse = (value) => ({
          ...value,
          errors:
            value.errors &&
            formatApolloErrors([...value.errors], {
              formatter: apolloServer.requestOptions.formatError,
              debug: apolloServer.requestOptions.debug
            })
        });

        let context;
        try {
          context = await apolloServer.context({ connection, payload: message.payload });
        } catch (error) {
          throw formatApolloErrors([error], {
            formatter: apolloServer.requestOptions.formatError,
            debug: apolloServer.requestOptions.debug
          })[0];
        }

        return { ...connection, context };
      }
    },
github apollographql / apollo-server / packages / apollo-server-core / src / ApolloServer.ts View on Github external
connection.formatResponse = (value: ExecutionResult) => ({
            ...value,
            errors:
              value.errors &&
              formatApolloErrors([...value.errors], {
                formatter: this.requestOptions.formatError,
                debug: this.requestOptions.debug,
              }),
          });
          let context: Context = this.context ? this.context : { connection };
github apollographql / apollo-server / packages / apollo-server-core / src / requestPipeline.ts View on Github external
function formatErrors(
    errors: ReadonlyArray,
  ): ReadonlyArray {
    return formatApolloErrors(errors, {
      formatter: config.formatError,
      debug: requestContext.debug,
    });
  }