Skip to content

Commit 312b6a5

Browse files
committed
Improve error handling for circular error objs
1 parent 0e8676f commit 312b6a5

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

src/common/log.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ if (__DEV__ && __SERVER__) {
1010
if (arguments.length == 1 && typeof arguments[0] === 'string' && arguments[0].match(/^\[(HMR|WDS)\]/)) {
1111
console_log('backend ' + arguments[0]);
1212
} else {
13-
console_log.apply(console_log, arguments);
13+
console_log.apply(global.console, arguments);
1414
}
1515
};
16+
17+
// let console_err = global.console.error;
18+
// global.console.error = function() {
19+
// arguments[0] = 'ce ' + new Error().stack + '\n\n\n' + arguments[0];
20+
// console_err.apply(global.console, arguments);
21+
// };
1622
}
1723

1824
export default log;

src/server/api/schema.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { makeExecutableSchema, addErrorLoggingToSchema } from 'graphql-tools';
1+
import { makeExecutableSchema } from 'graphql-tools';
22

33
import rootSchemaDef from './rootSchema.graphqls';
44
import modules from '../modules';
55
import pubsub from './pubsub';
6-
import log from '../../common/log';
76

87
const executableSchema = makeExecutableSchema({
98
typeDefs: [rootSchemaDef].concat(modules.schemas),
109
resolvers: modules.createResolvers(pubsub)
1110
});
1211

13-
addErrorLoggingToSchema(executableSchema, { log: e => log.error(e) });
14-
1512
export default executableSchema;

src/server/middleware/error.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
import path from 'path';
22
import fs from 'fs';
3+
import serialize from 'serialize-javascript';
34
import log from '../../common/log';
45
import { options as spinConfig } from '../../../.spinrc.json';
56

67
let assetMap;
78

9+
const stripCircular = (from, seen) => {
10+
const to = Array.isArray(from) ? [] : {};
11+
seen = seen || [];
12+
seen.push(from);
13+
Object.getOwnPropertyNames(from).forEach(key => {
14+
if (!from[key] || (typeof from[key] !== 'object' && !Array.isArray(from[key]))) {
15+
to[key] = from[key];
16+
} else if (seen.indexOf(from[key]) < 0) {
17+
to[key] = stripCircular(from[key], seen.slice(0));
18+
} else to[key] = '[Circular]';
19+
});
20+
return to;
21+
};
22+
823
/*
924
* The code below MUST be declared as a function, not closure,
1025
* otherwise Express will fail to execute this handler
@@ -16,10 +31,7 @@ function errorMiddleware(e, req, res, next) {
1631
assetMap = JSON.parse(fs.readFileSync(path.join(spinConfig.frontendBuildDir, 'web', 'assets.json')));
1732
}
1833

19-
const serverErrorScript = `<script charset="UTF-8">window.__SERVER_ERROR__=${JSON.stringify(
20-
e,
21-
Object.getOwnPropertyNames(e)
22-
)};</script>`;
34+
const serverErrorScript = `<script charset="UTF-8">window.__SERVER_ERROR__=${serialize(stripCircular(e))};</script>`;
2335
const vendorScript = assetMap['vendor.js'] ? `<script src="/${assetMap['vendor.js']}" charSet="utf-8"></script>` : '';
2436

2537
res.status(200).send(

src/server/middleware/graphql.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ export default graphqlExpress(async (req, res, next) => {
1010
return {
1111
schema,
1212
context: await modules.createContext(req, res),
13-
tracing: !!settings.analytics.apolloEngine.key,
14-
cacheControl: !!settings.analytics.apolloEngine.key
13+
debug: false,
14+
tracing: !!settings.engine.engineConfig.apiKey,
15+
cacheControl: !!settings.engine.engineConfig.apiKey
1516
};
1617
} catch (e) {
1718
next(e);

src/server/middleware/website.jsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,16 @@ const { protocol, hostname, port, pathname } = url.parse(__BACKEND_URL__);
3030
const apiUrl = `${protocol}//${hostname}:${process.env.PORT || port}${pathname}`;
3131

3232
const renderServerSide = async (req, res) => {
33+
console.log('req.path:', req.path);
3334
// if (__PERSIST_GQL__) {
3435
// networkInterface = addPersistedQueries(networkInterface, queryMap);
3536
// }
3637
//
3738

3839
const fetch = createApolloFetch({ uri: apiUrl, constructOptions: modules.constructFetchOptions });
3940
fetch.batchUse(({ options }, next) => {
40-
try {
41-
options.credentials = 'include';
42-
options.headers = req.headers;
43-
} catch (e) {
44-
console.error(e);
45-
}
41+
options.credentials = 'include';
42+
options.headers = req.headers;
4643

4744
next();
4845
});

0 commit comments

Comments
 (0)