From bb8a695aeec98bc89961ad20eafe2e96540143e4 Mon Sep 17 00:00:00 2001 From: Eric Dobbertin Date: Sun, 11 Jun 2017 20:39:57 -0500 Subject: [PATCH] Adjust client safe error handling based on PR review --- packages/ddp-server/livedata_server.js | 15 ++++++++------- packages/meteor/errors.js | 10 +++++++--- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/ddp-server/livedata_server.js b/packages/ddp-server/livedata_server.js index e9444d922f..cc8831304f 100644 --- a/packages/ddp-server/livedata_server.js +++ b/packages/ddp-server/livedata_server.js @@ -1724,13 +1724,14 @@ var wrapInternalException = function (exception, context) { // depend on the Meteor.Error class, `isClientSafe` can be set to true on any // error before it is thrown. if (exception.isClientSafe) { - const originalMessage = exception.message; - exception = new Meteor.Error(exception.error, exception.reason, exception.details); - exception.message = originalMessage; + if (!(exception instanceof Meteor.Error)) { + const originalMessage = exception.message; + exception = new Meteor.Error(exception.error, exception.reason, exception.details); + exception.message = originalMessage; + } + return exception; } - if (exception instanceof Meteor.Error) return exception; - // tests can set the 'expected' flag on an exception so it won't go to the // server log if (!exception.expected) { @@ -1746,10 +1747,10 @@ var wrapInternalException = function (exception, context) { // provided a "sanitized" version with more context than 500 Internal server // error? Use that. if (exception.sanitizedError) { - if (exception.sanitizedError instanceof Meteor.Error) + if (exception.sanitizedError.isClientSafe) return exception.sanitizedError; Meteor._debug("Exception " + context + " provides a sanitizedError that " + - "is not a Meteor.Error; ignoring"); + "does not have isClientSafe property set; ignoring"); } return new Meteor.Error(500, "Internal server error"); diff --git a/packages/meteor/errors.js b/packages/meteor/errors.js index dfff723900..01a9d50d23 100644 --- a/packages/meteor/errors.js +++ b/packages/meteor/errors.js @@ -47,7 +47,7 @@ Meteor.makeErrorType = function (name, constructor) { * ``` * // on the server, pick a code unique to this error * // the reason field should be a useful debug message - * throw new Meteor.Error("logged-out", + * throw new Meteor.Error("logged-out", * "The user must be logged in to post a comment."); * * // on the client @@ -59,10 +59,10 @@ Meteor.makeErrorType = function (name, constructor) { * } * }); * ``` - * + * * For legacy reasons, some built-in Meteor functions such as `check` throw * errors with a number in this field. - * + * * @param {String} [reason] Optional. A short human-readable summary of the * error, like 'Not Found'. * @param {String} [details] Optional. Additional information about the error, @@ -73,6 +73,10 @@ Meteor.Error = Meteor.makeErrorType( function (error, reason, details) { var self = this; + // Newer versions of DDP use this property to signify that an error + // can be sent back and reconstructed on the calling client. + self.isClientSafe = true; + // String code uniquely identifying this kind of error. self.error = error;