fix odd case in error handling

This commit is contained in:
James Baxley 2018-04-18 09:56:02 -04:00 committed by Evans Hauser
parent e75fbf4b53
commit bdabe6259b
No known key found for this signature in database
GPG key ID: 88AF586817F52EEC
7 changed files with 21 additions and 26 deletions

View file

@ -16,6 +16,7 @@ export function formatError(error: GraphQLError, debug: boolean = false) {
...error.extensions,
code: (error.extensions && error.extensions.code) || 'INTERNAL_ERROR',
exception: {
...(error.extensions && error.extensions.exception),
...(error.originalError as any),
},
},
@ -25,7 +26,7 @@ export function formatError(error: GraphQLError, debug: boolean = false) {
//graphql-js ensures that the originalError's extensions are hoisted
//https://github.com/graphql/graphql-js/blob/0bb47b2/src/error/GraphQLError.js#L138
delete expanded.extensions.exception.extensions;
if (debug) {
if (debug && !expanded.extensions.exception.stacktrace) {
expanded.extensions.exception.stacktrace =
(error.originalError &&
error.originalError.stack &&
@ -35,7 +36,7 @@ export function formatError(error: GraphQLError, debug: boolean = false) {
if (Object.keys(expanded.extensions.exception).length === 0) {
//remove from printing an empty object
expanded.extensions.exception = undefined;
delete expanded.extensions.exception;
}
return expanded;

View file

@ -16,4 +16,5 @@ export {
ParseError,
ValidationError,
AuthenticationError,
formatError,
} from './errors';

View file

@ -7,7 +7,7 @@ export * from './utils/exports';
import { ApolloServer as ExpressServer } from './express';
export class ApolloServer<Context> extends ExpressServer {
constructor(opts: Config<express.Application, Context>) {
constructor(opts: Config<express.Application, express.Request, Context>) {
opts.app = express();
super(opts);
super.applyMiddleware();

View file

@ -1,7 +1,13 @@
import { makeExecutableSchema } from 'graphql-tools';
import { Server as HttpServer } from 'http';
import { execute, GraphQLSchema, subscribe, ExecutionResult } from 'graphql';
import {
execute,
GraphQLSchema,
subscribe,
ExecutionResult,
GraphQLError,
} from 'graphql';
import { formatError } from 'apollo-server-core';
import { ApolloEngine as Engine } from 'apollo-engine';
import {
SubscriptionServer,
@ -19,8 +25,6 @@ import {
ContextFunction,
} from './types';
import { formatError } from './errors';
// taken from engine
export function joinHostPort(host: string, port: number) {
if (host.includes(':')) host = `[${host}]`;
@ -43,6 +47,7 @@ export class ApolloServerBase<
private graphqlEndpoint: string = '/graphql';
private cors?: Cors;
private engineEnabled: boolean = false;
private debug?: boolean;
constructor(config: Config<Server, Request, Cors>) {
const {
@ -56,8 +61,10 @@ export class ApolloServerBase<
engineInRequestPath,
subscriptions,
cors,
debug,
} = config;
this.debug = debug;
this.context = context;
// XXX should we move this to the `start` call? This would make hot
// reloading eaiser but may not be worth it?
@ -236,7 +243,7 @@ ApolloServer was unable to load the configuration for Apollo Engine. Please veri
onOperation: async (_: string, connection: ExecutionParams) => {
connection.formatResponse = (value: ExecutionResult) => ({
...value,
errors: value.errors && value.errors.map(formatError),
errors: value.errors && value.errors.map(err => formatError(err)),
});
let context: Context = this.context ? this.context : { connection };
@ -285,7 +292,8 @@ when calling this.request, either call it using an error function, or bind it li
schema: this.schema,
tracing: Boolean(this.engineEnabled),
cacheControl: Boolean(this.engineEnabled),
// formatError,
formatError: (e: GraphQLError) => formatError(e, Boolean(this.debug)),
debug: Boolean(this.debug),
context,
};
}

View file

@ -26,6 +26,7 @@ export interface Config<Server, ContextShape = any, Cors = CorsOptions> {
subscriptions?: SubscriptionServerOptions | string | false;
engineInRequestPath?: boolean;
engine?: boolean | Object;
debug?: boolean;
}
export interface EngineConfig {

View file

@ -10,6 +10,6 @@
"noUnusedLocals": true,
"noImplicitAny": true
},
"include": ["src/**/*", "types/**"],
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}

View file

@ -1,16 +0,0 @@
declare module 'ink' {
import { createElement, Component as ReactComponent } from 'react';
export class Component<P, S> extends ReactComponent<P, S> {}
export const h: typeof createElement;
export function renderToString(tree: JSX.Element): string;
export function render(
tree: JSX.Element,
prevTree?: JSX.Element,
): JSX.Element;
export function mount(
tree: JSX.Element,
stream: NodeJS.WritableStream,
): () => void;
}