Skip to content

Log ParseErrors and Prevent Express from Logging error object #3431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/CloudCodeLogger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ describe("Cloud Code Logger", () => {
Parse.Cloud.run('aFunction', { foo: 'bar' })
.then(null, () => logController.getLogs({ from: Date.now() - 500, size: 1000 }))
.then(logs => {
const log = logs[1];
const log = logs[2];
expect(log.level).toEqual('error');
expect(log.message).toMatch(
/Failed running cloud function aFunction for user [^ ]* with:\n {2}Input: {"foo":"bar"}\n {2}Error: {"code":141,"message":"it failed!"}/);
Expand Down
38 changes: 34 additions & 4 deletions spec/FilesController.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
var GridStoreAdapter = require("../src/Adapters/Files/GridStoreAdapter").GridStoreAdapter;
var Config = require("../src/Config");
var FilesController = require('../src/Controllers/FilesController').default;
const LoggerController = require('../src/Controllers/LoggerController').LoggerController;
const WinstonLoggerAdapter = require('../src/Adapters/Logger/WinstonLoggerAdapter').WinstonLoggerAdapter;
const GridStoreAdapter = require("../src/Adapters/Files/GridStoreAdapter").GridStoreAdapter;
const Config = require("../src/Config");
const FilesController = require('../src/Controllers/FilesController').default;

const mockAdapter = {
createFile: () => {
return Parse.Promise.reject(new Error('it failed'));
},
deleteFile: () => { },
getFileData: () => { },
getFileLocation: () => 'xyz'
}

// Small additional tests to improve overall coverage
describe("FilesController",() =>{
Expand All @@ -26,5 +36,25 @@ describe("FilesController",() =>{
expect(anObject.aFile.url).toEqual("http://an.url");

done();
})
});

it('should create a server log on failure', done => {
const logController = new LoggerController(new WinstonLoggerAdapter());

reconfigureServer({ filesAdapter: mockAdapter })
.then(() => new Promise(resolve => setTimeout(resolve, 1000)))
.then(() => new Parse.File("yolo.txt", [1,2,3], "text/plain").save())
.then(
() => done.fail('should not succeed'),
() => setImmediate(() => Parse.Promise.as('done'))
)
.then(() => logController.getLogs({ from: Date.now() - 500, size: 1000 }))
.then((logs) => {
const log = logs.pop();
expect(log.level).toBe('error');
expect(log.code).toBe(130);
expect(log.message).toBe('Could not store file.');
done();
});
});
});
31 changes: 17 additions & 14 deletions src/middlewares.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import AppCache from './cache';
import log from './logger';
import Parse from 'parse/node';
import auth from './Auth';
import Config from './Config';
import ClientSDK from './ClientSDK';
import AppCache from './cache';
import log from './logger';
import Parse from 'parse/node';
import auth from './Auth';
import Config from './Config';
import ClientSDK from './ClientSDK';

// Checks that the request is authorized for this app and checks user
// auth too.
Expand Down Expand Up @@ -233,10 +233,8 @@ export function allowMethodOverride(req, res, next) {
}

export function handleParseErrors(err, req, res, next) {
// TODO: Add logging as those errors won't make it to the PromiseRouter
if (err instanceof Parse.Error) {
var httpStatus;

let httpStatus;
// TODO: fill out this mapping
switch (err.code) {
case Parse.Error.INTERNAL_SERVER_ERROR:
Expand All @@ -250,17 +248,22 @@ export function handleParseErrors(err, req, res, next) {
}

res.status(httpStatus);
res.json({code: err.code, error: err.message});
res.json({ code: err.code, error: err.message });
log.error(err.message, err);
} else if (err.status && err.message) {
res.status(err.status);
res.json({error: err.message});
res.json({ error: err.message });
next(err);
} else {
log.error('Uncaught internal server error.', err, err.stack);
res.status(500);
res.json({code: Parse.Error.INTERNAL_SERVER_ERROR,
message: 'Internal server error.'});
res.json({
code: Parse.Error.INTERNAL_SERVER_ERROR,
message: 'Internal server error.'
});
next(err);
}
next(err);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this will lead to next() not being called in certain situations

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is intentional. in the case that we have a ParseServer.Error, we handle logging the error. So we don't call next(err) which then go to the Express' error logging backstop: https://github.com/expressjs/express/blob/3c54220a3495a7a2cdf580c3289ee37e835c0190/lib/application.js#L161 which is what leads to the [object, object] getting emitted to console.error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should add that my experience with express is pretty limited, so if there would be a bad side affect of not calling next(err), don't assume that I have contemplated that. but my thought is that we've taken care of the final step in this failed request which is to log it, so next isn't required.

}

export function enforceMasterKeyAccess(req, res, next) {
Expand Down