From 9d28329cb45549e76bf3a5485db1f0f45b179d73 Mon Sep 17 00:00:00 2001 From: Victor Vlasenko Date: Wed, 29 Nov 2017 09:22:22 +0200 Subject: [PATCH] Improve error handling for circular error objs --- src/common/log.js | 8 +++++++- src/server/api/schema.js | 5 +---- src/server/middleware/error.js | 20 ++++++++++++++++---- src/server/middleware/graphql.js | 5 +++-- src/server/middleware/website.jsx | 8 ++------ 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/common/log.js b/src/common/log.js index fe3bff6ce1..a04673db4a 100644 --- a/src/common/log.js +++ b/src/common/log.js @@ -10,9 +10,15 @@ if (__DEV__ && __SERVER__) { if (arguments.length == 1 && typeof arguments[0] === 'string' && arguments[0].match(/^\[(HMR|WDS)\]/)) { console_log('backend ' + arguments[0]); } else { - console_log.apply(console_log, arguments); + console_log.apply(global.console, arguments); } }; + + // let console_err = global.console.error; + // global.console.error = function() { + // arguments[0] = 'ce ' + new Error().stack + '\n\n\n' + arguments[0]; + // console_err.apply(global.console, arguments); + // }; } export default log; diff --git a/src/server/api/schema.js b/src/server/api/schema.js index d9adbfc9ba..b413ca874c 100644 --- a/src/server/api/schema.js +++ b/src/server/api/schema.js @@ -1,15 +1,12 @@ -import { makeExecutableSchema, addErrorLoggingToSchema } from 'graphql-tools'; +import { makeExecutableSchema } from 'graphql-tools'; import rootSchemaDef from './rootSchema.graphqls'; import modules from '../modules'; import pubsub from './pubsub'; -import log from '../../common/log'; const executableSchema = makeExecutableSchema({ typeDefs: [rootSchemaDef].concat(modules.schemas), resolvers: modules.createResolvers(pubsub) }); -addErrorLoggingToSchema(executableSchema, { log: e => log.error(e) }); - export default executableSchema; diff --git a/src/server/middleware/error.js b/src/server/middleware/error.js index 4335a0f230..0a5714ec40 100644 --- a/src/server/middleware/error.js +++ b/src/server/middleware/error.js @@ -1,10 +1,25 @@ import path from 'path'; import fs from 'fs'; +import serialize from 'serialize-javascript'; import log from '../../common/log'; import { options as spinConfig } from '../../../.spinrc.json'; let assetMap; +const stripCircular = (from, seen) => { + const to = Array.isArray(from) ? [] : {}; + seen = seen || []; + seen.push(from); + Object.getOwnPropertyNames(from).forEach(key => { + if (!from[key] || (typeof from[key] !== 'object' && !Array.isArray(from[key]))) { + to[key] = from[key]; + } else if (seen.indexOf(from[key]) < 0) { + to[key] = stripCircular(from[key], seen.slice(0)); + } else to[key] = '[Circular]'; + }); + return to; +}; + /* * The code below MUST be declared as a function, not closure, * otherwise Express will fail to execute this handler @@ -16,10 +31,7 @@ function errorMiddleware(e, req, res, next) { assetMap = JSON.parse(fs.readFileSync(path.join(spinConfig.frontendBuildDir, 'web', 'assets.json'))); } - const serverErrorScript = ``; + const serverErrorScript = ``; const vendorScript = assetMap['vendor.js'] ? `` : ''; res.status(200).send( diff --git a/src/server/middleware/graphql.js b/src/server/middleware/graphql.js index 5c3c03c163..d6483b02b6 100644 --- a/src/server/middleware/graphql.js +++ b/src/server/middleware/graphql.js @@ -10,8 +10,9 @@ export default graphqlExpress(async (req, res, next) => { return { schema, context: await modules.createContext(req, res), - tracing: !!settings.analytics.apolloEngine.key, - cacheControl: !!settings.analytics.apolloEngine.key + debug: false, + tracing: !!settings.engine.engineConfig.apiKey, + cacheControl: !!settings.engine.engineConfig.apiKey }; } catch (e) { next(e); diff --git a/src/server/middleware/website.jsx b/src/server/middleware/website.jsx index 161e090c4a..efc697fa4c 100644 --- a/src/server/middleware/website.jsx +++ b/src/server/middleware/website.jsx @@ -37,12 +37,8 @@ const renderServerSide = async (req, res) => { const fetch = createApolloFetch({ uri: apiUrl, constructOptions: modules.constructFetchOptions }); fetch.batchUse(({ options }, next) => { - try { - options.credentials = 'include'; - options.headers = req.headers; - } catch (e) { - console.error(e); - } + options.credentials = 'include'; + options.headers = req.headers; next(); });