Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
cleaning up how errors are extended. Closes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
ekryski committed Jan 12, 2016
1 parent 44796fe commit bc926de
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"plugins": [
"transform-object-assign",
"add-module-exports"
"add-module-exports",
["babel-plugin-transform-builtin-extend", { globals: ["Error", "Array"] }]
],
"presets": [ "es2015" ]
}
89 changes: 69 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

Object.defineProperty(exports, "__esModule", {
value: true
});
Expand All @@ -11,15 +13,42 @@ var _debug2 = _interopRequireDefault(_debug);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }
function _extendableBuiltin(cls) {
function ExtendableBuiltin() {
var instance = Reflect.construct(cls, Array.from(arguments));
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
return instance;
}

require('babel-polyfill');
ExtendableBuiltin.prototype = Object.create(cls.prototype, {
constructor: {
value: cls,
enumerable: false,
writable: true,
configurable: true
}
});

if (Object.setPrototypeOf) {
Object.setPrototypeOf(ExtendableBuiltin, cls);
} else {
ExtendableBuiltin.__proto__ = cls;
}

return ExtendableBuiltin;
}

if (!global._babelPolyfill) {
require('babel-polyfill');
}

var debug = (0, _debug2.default)('feathers-errors');

Expand All @@ -28,8 +57,14 @@ var debug = (0, _debug2.default)('feathers-errors');
// Node v5.0+ does support this but until we want to drop support
// for older versions we need this hack.
// http://stackoverflow.com/questions/33870684/why-doesnt-instanceof-work-on-instances-of-error-subclasses-under-babel-node
function AbstractError(klass) {
function Constructor(msg, name, code, className, data) {
// https://github.com/loganfsmyth/babel-plugin-transform-builtin-extend

var FeathersError = (function (_extendableBuiltin2) {
_inherits(FeathersError, _extendableBuiltin2);

function FeathersError(msg, name, code, className, data) {
_classCallCheck(this, FeathersError);

msg = msg || 'Error';

var errors = undefined;
Expand All @@ -53,8 +88,6 @@ function AbstractError(klass) {
message = msg;
}

klass.call(this, msg);

if (data && data.errors) {
errors = data.errors;
delete data.errors;
Expand All @@ -63,25 +96,41 @@ function AbstractError(klass) {
// NOTE (EK): Babel doesn't support this so
// we have to pass in the class name manually.
// this.name = this.constructor.name;
this.name = name;
this.message = message;
this.code = code;
this.className = className;
this.data = data;
this.errors = errors || {};

Error.captureStackTrace(this, this.name);
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(FeathersError).call(this, message));

debug(this.name + '(' + this.code + '): ' + this.message);
}
_this.name = name;
_this.message = message;
_this.code = code;
_this.className = className;
_this.data = data;
_this.errors = errors || {};

AbstractError.prototype = Object.create(klass.prototype);
Object.setPrototypeOf(AbstractError, klass);
// Error.captureStackTrace(this, this.name);

return Constructor;
}
debug(_this.name + '(' + _this.code + '): ' + _this.message);
return _this;
}

// NOTE (EK): A little hack to get around `message` not
// being included in the default toJSON call.

_createClass(FeathersError, [{
key: 'toJSON',
value: function toJSON() {
return {
name: this.name,
message: this.message,
code: this.code,
className: this.className,
data: this.data,
errors: this.errors
};
}
}]);

var FeathersError = AbstractError(Error);
return FeathersError;
})(_extendableBuiltin(Error));

var BadRequest = (function (_FeathersError) {
_inherits(BadRequest, _FeathersError);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"babel-cli": "^6.3.17",
"babel-core": "^6.3.17",
"babel-plugin-add-module-exports": "^0.1.1",
"babel-plugin-transform-builtin-extend": "^1.1.0",
"babel-plugin-transform-object-assign": "^6.3.13",
"babel-preset-es2015": "^6.3.13",
"feathers": "^1.2.0",
Expand Down
30 changes: 18 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ const debug = makeDebug('feathers-errors');
// Node v5.0+ does support this but until we want to drop support
// for older versions we need this hack.
// http://stackoverflow.com/questions/33870684/why-doesnt-instanceof-work-on-instances-of-error-subclasses-under-babel-node
function AbstractError(klass) {
function Constructor(msg, name, code, className, data) {
// https://github.com/loganfsmyth/babel-plugin-transform-builtin-extend

class FeathersError extends Error {
constructor(msg, name, code, className, data) {
msg = msg || 'Error';

let errors;
Expand All @@ -34,13 +36,13 @@ function AbstractError(klass) {
message = msg;
}

klass.call(this, msg);

if (data && data.errors) {
errors = data.errors;
delete data.errors;
}

super(message);

// NOTE (EK): Babel doesn't support this so
// we have to pass in the class name manually.
// this.name = this.constructor.name;
Expand All @@ -51,19 +53,23 @@ function AbstractError(klass) {
this.data = data;
this.errors = errors || {};

Error.captureStackTrace(this, this.name);

debug(`${this.name}(${this.code}): ${this.message}`);
}

AbstractError.prototype = Object.create(klass.prototype);
Object.setPrototypeOf(AbstractError, klass);

return Constructor;
// NOTE (EK): A little hack to get around `message` not
// being included in the default toJSON call.
toJSON() {
return {
name: this.name,
message: this.message,
code: this.code,
className: this.className,
data: this.data,
errors: this.errors
};
}
}

let FeathersError = AbstractError(Error);

class BadRequest extends FeathersError {
constructor(message, data) {
super(message, 'BadRequest', 400, 'bad-request', data);
Expand Down

0 comments on commit bc926de

Please sign in to comment.