Refactor Avi's improved Meteor.Error to make it possible to use for other Error

subclasses.
This commit is contained in:
David Glasser
2013-04-11 17:11:45 -07:00
parent 654923abae
commit d1dd8a8e4d
3 changed files with 65 additions and 48 deletions

View File

@@ -119,55 +119,32 @@ Meteor._CurrentInvocation = new Meteor.EnvironmentVariable;
// The DDP client manually puts these into Meteor.Error objects. (We don't use
// EJSON.addType here because the type is determined by location in the
// protocol, not text on the wire.)
Meteor.Error = function (error, reason, details) {
var self = this;
Meteor.Error = Meteor.makeErrorType(
"Meteor.Error",
function (error, reason, details) {
var self = this;
// Ensure we get a proper stack trace in most Javascript environments
if (Error.captureStackTrace) {
// V8 environments (Chrome and Node.js)
Error.captureStackTrace(self, Meteor.Error);
} else {
// Firefox
var e = new Error;
e.__proto__ = Meteor.Error.prototype;
if (e instanceof Meteor.Error)
self = e;
}
// Safari magically works.
// Currently, a numeric code, likely similar to a HTTP code (eg,
// 404, 500). That is likely to change though.
self.error = error;
// Currently, a numeric code, likely similar to a HTTP code (eg,
// 404, 500). That is likely to change though.
self.error = error;
// Optional: A short human-readable summary of the error. Not
// intended to be shown to end users, just developers. ("Not Found",
// "Internal Server Error")
self.reason = reason;
// Optional: A short human-readable summary of the error. Not
// intended to be shown to end users, just developers. ("Not Found",
// "Internal Server Error")
self.reason = reason;
// Optional: Additional information about the error, say for
// debugging. It might be a (textual) stack trace if the server is
// willing to provide one. The corresponding thing in HTTP would be
// the body of a 404 or 500 response. (The difference is that we
// never expect this to be shown to end users, only developers, so
// it doesn't need to be pretty.)
self.details = details;
// Optional: Additional information about the error, say for
// debugging. It might be a (textual) stack trace if the server is
// willing to provide one. The corresponding thing in HTTP would be
// the body of a 404 or 500 response. (The difference is that we
// never expect this to be shown to end users, only developers, so
// it doesn't need to be pretty.)
self.details = details;
// This is what gets displayed at the top of a stack trace. Current
// format is "[404]" (if no reason is set) or "File not found [404]"
if (self.reason)
self.message = self.reason + ' [' + self.error + ']';
else
self.message = '[' + self.error + ']';
return self;
};
// http://davidshariff.com/blog/javascript-inheritance-patterns/
var inherits = function (child, parent) {
var tmp = function () {};
tmp.prototype = parent.prototype;
child.prototype = new tmp;
child.prototype.constructor = child;
};
inherits(Meteor.Error, Error);
// This is what gets displayed at the top of a stack trace. Current
// format is "[404]" (if no reason is set) or "File not found [404]"
if (self.reason)
self.message = self.reason + ' [' + self.error + ']';
else
self.message = '[' + self.error + ']';
});

39
packages/meteor/errors.js Normal file
View File

@@ -0,0 +1,39 @@
// http://davidshariff.com/blog/javascript-inheritance-patterns/
var inherits = function (child, parent) {
var tmp = function () {};
tmp.prototype = parent.prototype;
child.prototype = new tmp;
child.prototype.constructor = child;
};
// Makes an error subclass which properly contains a stack trace in most
// environments. constructor can set fields on `this` (and should probably set
// `message`, which is what gets displayed at the top of a stack trace).
Meteor.makeErrorType = function (name, constructor) {
var errorClass = function (/*arguments*/) {
var self = this;
// Ensure we get a proper stack trace in most Javascript environments
if (Error.captureStackTrace) {
// V8 environments (Chrome and Node.js)
Error.captureStackTrace(self, errorClass);
} else {
// Firefox
var e = new Error;
e.__proto__ = errorClass.prototype;
if (e instanceof errorClass)
self = e;
}
// Safari magically works.
constructor.apply(self, arguments);
self.errorType = name;
return self;
};
inherits(errorClass, Error);
return errorClass;
};

View File

@@ -34,6 +34,7 @@ Package.on_use(function (api, where) {
api.add_files('server_environment.js', 'server');
api.add_files('helpers.js', ['client', 'server']);
api.add_files('timers.js', ['client', 'server']);
api.add_files('errors.js', ['client', 'server']);
api.add_files('fiber_helpers.js', 'server');
// dynamic variables, bindEnvironment