Parse-server: Handle exception from Cloud Code better

Created on 16 Mar 2016  路  11Comments  路  Source: parse-community/parse-server

Currently, all exceptions from Cloud Code are caught in Parse Express Middleware then what it does is:

  • Use console.log to log the error and its stack trace
  • Response to client: {code: Parse.Error.INTERNAL_SERVER_ERROR, message: 'Internal server error.'}

=> It is very difficult to debug the application and when an error happened. And on production, we usually don't know it because it's just a normal log.

My suggestion:

  1. At least, log the exception's info at error level, use console.error. Return the exception's trace to the client.
  2. Pass the exception to the next middleware (if available) so next middleware can handle it. This makes we can integrate exception tracking service better.

Any better suggestion?

P/s: You may want to have a look at function handleParseErrors in src/middlewares.js.

var handleParseErrors = function(err, req, res, next) {
  if (err instanceof Parse.Error) {
    var httpStatus;

    // TODO: fill out this mapping
    switch (err.code) {
    case Parse.Error.INTERNAL_SERVER_ERROR:
      httpStatus = 500;
      break;
    case Parse.Error.OBJECT_NOT_FOUND:
      httpStatus = 404;
      break;
    default:
      httpStatus = 400;
    }

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

Most helpful comment

How would one plug into this to report errors and environment data from Cloud Code functions to third-party error reporting services (like Sentry, Bugsnag, Rollbar, etc)?

All 11 comments

I seem to remember that original CloudCode (on Parse.com) had a pretty comprehensive failsafe evaluation of pushes to prevent erroneous code being deployed... I'm not sure we'd need to go that far, but right now this is a big cause of development drain for me, as having to check the logs on AWS every time I hit an exception is laborious...

I agree that returning the trace to the client (with perhaps even some kind of linting or syntactic warnings) would be immensely helpful.

What you can do in the mean time, is implement a winston logger for your cloud code, and use it with a logging facility like log entries.

https://www.npmjs.com/package/winston
https://www.npmjs.com/package/winston-logentries-simple

With winston, you could add custom exception handlers and have a better understanding of what's going on.

Then, in cloud code, you would get the default logger from winston, and use logger.info(...), logger.error()...

You can also explore https://www.npmjs.com/package/bunyan which is a powerful logging framework

@flovilmart For now, I do have my own logger but it can't catch all exception from Cloud Code. If I do it in your way, I will have to wrap each Cloud Function to a try-catch, right?. It would be better if we can catch all uncaught exception in one place. Do you have any idea to do that?

Read the docs https://www.npmjs.com/package/winston#handling-uncaught-exceptions-with-winston

@flovilmart seem that it is not what I wanted. I want to catch all uncaught exception thrown from Cloud Code and manage it by myself. Currently, Express will catch all uncaught exception and use console.log to log it out only and do nothing after that. Thus, Winston can't now catch exceptions which caught by Express.

sorry for the late response,

we've added log.error through winston: https://github.com/ParsePlatform/parse-server/blob/master/src/middlewares.js#L188

Exceptions should be logged accordingly now, I believe that commit already made it to the npm registry.

Does it help?

@flovilmart perfect :+1: I think It will work fine.

I you need anything else, don't hesitate to open another issue.

Super news! Great work :)

How would one plug into this to report errors and environment data from Cloud Code functions to third-party error reporting services (like Sentry, Bugsnag, Rollbar, etc)?

Not the clean solution of course, but super fast would be just to modify the _middlewares.js_ file. Just manually create an error in the beginning of handleParseErrors function and than print stack and pass the error.stack to the client
ex:

var handleParseErrors = function handleParseErrors(err, req, res, next) {
  var error = new Error();
  console.log("**** stack trace:");
  console.log(error.stack);
  ........
  if (err instanceof Parse.Error) {
    ......
    res.json({ code: err.code, error: err.message, stack: error.stack });
  } else if (err.status && err.message) {
    res.status(err.status);
    res.json({ error: err.message, stack: error.stack });
  }
.......
};
Was this page helpful?
0 / 5 - 0 ratings