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

Es6 class fix #15

Merged
merged 3 commits into from
Dec 14, 2015
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '0.12'
- 'node'
- 'iojs'
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,20 @@ Feathers errors come with feathers by default. So typically you don't need to in
```js
import errors from 'feathers-errors';

let userService = {
find(params, callback) {

// If you were to create an error yourself.
callback(new errors.NotFound('User does not exist'));

// You can also simply do something like this if you
// just want to fire back a simple 500 error with your
// custom message.
//
// callback('A generic server error');
}
};
// If you were to create an error yourself.
var notFound = new errors.NotFound('User does not exist'));

// You can wrap existing errors
var existing = new errors.GeneralError(new Error('I exist'));

// You can also pass additional data
var data = new errors.BadRequest('Invalid email', {email: 'sergey@google.com'});
```

## Release History
__1.0.1__
- Fixing critical bug [#15](https://github.com/feathersjs/feathers-errors/issues/15)

__1.0.0__
- converting to ES6
- making structure consistent with other plugins
Expand Down
79 changes: 40 additions & 39 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ 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; } // import util from 'util';
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; }

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

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

function AbstractError(msg, name, code, className, data) {
_classCallCheck(this, AbstractError);
var debug = (0, _debug2.default)('feathers-errors');

// NOTE (EK): Babel doesn't properly support extending
// some classes in ES6. The Error class being one of them.
// 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) {
msg = msg || 'Error';

var errors = undefined;
Expand All @@ -35,9 +36,7 @@ var AbstractError = (function (_Error) {
if (msg instanceof Error) {
message = msg.message || 'Error';

// NOTE (EK): This is typically to handle errors
// that are thrown from other modules. For example,
// Mongoose validations can return multiple errors.
// NOTE (EK): This is typically to handle validation errors
if (msg.errors) {
errors = msg.errors;
}
Expand All @@ -46,33 +45,35 @@ var AbstractError = (function (_Error) {
else if ((typeof msg === 'undefined' ? 'undefined' : _typeof(msg)) === 'object') {
message = msg.message || 'Error';
errors = msg.errors ? msg.errors : msg;
data = msg;
}
// message is just a string
else {
message = msg;
}

klass.call(this, msg);

// 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;

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(AbstractError).call(this, message));

_this.name = name;
_this.message = message;
_this.code = code;
_this.className = className;
_this.data = data;
_this.errors = errors;
Error.captureStackTrace(this, this.name);

Error.captureStackTrace(_this, _this.name);

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

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

return Constructor;
}

var BadRequest = (function (_AbstractError) {
_inherits(BadRequest, _AbstractError);
Expand All @@ -84,7 +85,7 @@ var BadRequest = (function (_AbstractError) {
}

return BadRequest;
})(AbstractError);
})(AbstractError(Error));

var NotAuthenticated = (function (_AbstractError2) {
_inherits(NotAuthenticated, _AbstractError2);
Expand All @@ -96,7 +97,7 @@ var NotAuthenticated = (function (_AbstractError2) {
}

return NotAuthenticated;
})(AbstractError);
})(AbstractError(Error));

var PaymentError = (function (_AbstractError3) {
_inherits(PaymentError, _AbstractError3);
Expand All @@ -108,7 +109,7 @@ var PaymentError = (function (_AbstractError3) {
}

return PaymentError;
})(AbstractError);
})(AbstractError(Error));

var Forbidden = (function (_AbstractError4) {
_inherits(Forbidden, _AbstractError4);
Expand All @@ -120,7 +121,7 @@ var Forbidden = (function (_AbstractError4) {
}

return Forbidden;
})(AbstractError);
})(AbstractError(Error));

var NotFound = (function (_AbstractError5) {
_inherits(NotFound, _AbstractError5);
Expand All @@ -132,7 +133,7 @@ var NotFound = (function (_AbstractError5) {
}

return NotFound;
})(AbstractError);
})(AbstractError(Error));

var MethodNotAllowed = (function (_AbstractError6) {
_inherits(MethodNotAllowed, _AbstractError6);
Expand All @@ -144,7 +145,7 @@ var MethodNotAllowed = (function (_AbstractError6) {
}

return MethodNotAllowed;
})(AbstractError);
})(AbstractError(Error));

var NotAcceptable = (function (_AbstractError7) {
_inherits(NotAcceptable, _AbstractError7);
Expand All @@ -156,7 +157,7 @@ var NotAcceptable = (function (_AbstractError7) {
}

return NotAcceptable;
})(AbstractError);
})(AbstractError(Error));

var Timeout = (function (_AbstractError8) {
_inherits(Timeout, _AbstractError8);
Expand All @@ -168,7 +169,7 @@ var Timeout = (function (_AbstractError8) {
}

return Timeout;
})(AbstractError);
})(AbstractError(Error));

var Conflict = (function (_AbstractError9) {
_inherits(Conflict, _AbstractError9);
Expand All @@ -180,7 +181,7 @@ var Conflict = (function (_AbstractError9) {
}

return Conflict;
})(AbstractError);
})(AbstractError(Error));

var Unprocessable = (function (_AbstractError10) {
_inherits(Unprocessable, _AbstractError10);
Expand All @@ -192,7 +193,7 @@ var Unprocessable = (function (_AbstractError10) {
}

return Unprocessable;
})(AbstractError);
})(AbstractError(Error));

var GeneralError = (function (_AbstractError11) {
_inherits(GeneralError, _AbstractError11);
Expand All @@ -204,7 +205,7 @@ var GeneralError = (function (_AbstractError11) {
}

return GeneralError;
})(AbstractError);
})(AbstractError(Error));

var NotImplemented = (function (_AbstractError12) {
_inherits(NotImplemented, _AbstractError12);
Expand All @@ -216,7 +217,7 @@ var NotImplemented = (function (_AbstractError12) {
}

return NotImplemented;
})(AbstractError);
})(AbstractError(Error));

var Unavailable = (function (_AbstractError13) {
_inherits(Unavailable, _AbstractError13);
Expand All @@ -228,7 +229,7 @@ var Unavailable = (function (_AbstractError13) {
}

return Unavailable;
})(AbstractError);
})(AbstractError(Error));

var errors = {
BadRequest: BadRequest,
Expand Down
48 changes: 28 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// import util from 'util';
import makeDebug from 'debug';

const debug = makeDebug('feathers-errors');

class AbstractError extends Error {
constructor(msg, name, code, className, data) {
// NOTE (EK): Babel doesn't properly support extending
// some classes in ES6. The Error class being one of them.
// 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) {
msg = msg || 'Error';

let errors;
Expand All @@ -13,9 +17,7 @@ class AbstractError extends Error {
if (msg instanceof Error) {
message = msg.message || 'Error';

// NOTE (EK): This is typically to handle errors
// that are thrown from other modules. For example,
// Mongoose validations can return multiple errors.
// NOTE (EK): This is typically to handle validation errors
if (msg.errors) {
errors = msg.errors;
}
Expand All @@ -24,13 +26,14 @@ class AbstractError extends Error {
else if (typeof msg === 'object') {
message = msg.message || 'Error';
errors = msg.errors ? msg.errors : msg;
data = msg;
}
// message is just a string
else {
message = msg;
}

super(message);
klass.call(this, msg);

// NOTE (EK): Babel doesn't support this so
// we have to pass in the class name manually.
Expand All @@ -46,81 +49,86 @@ class AbstractError extends Error {

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

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

return Constructor;
}

class BadRequest extends AbstractError {
class BadRequest extends AbstractError(Error) {
constructor(message, data) {
super(message, 'BadRequest', 400, 'bad-request', data);
}
}

class NotAuthenticated extends AbstractError {
class NotAuthenticated extends AbstractError(Error) {
constructor(message, data) {
super(message, 'NotAuthenticated', 401, 'not-authenticated', data);
}
}

class PaymentError extends AbstractError {
class PaymentError extends AbstractError(Error) {
constructor(message, data) {
super(message, 'PaymentError', 402, 'payment-error', data);
}
}

class Forbidden extends AbstractError {
class Forbidden extends AbstractError(Error) {
constructor(message, data) {
super(message, 'Forbidden', 403, 'forbidden', data);
}
}

class NotFound extends AbstractError {
class NotFound extends AbstractError(Error) {
constructor(message, data) {
super(message, 'NotFound', 404, 'not-found', data);
}
}

class MethodNotAllowed extends AbstractError {
class MethodNotAllowed extends AbstractError(Error) {
constructor(message, data) {
super(message, 'MethodNotAllowed', 405, 'method-not-allowed', data);
}
}

class NotAcceptable extends AbstractError {
class NotAcceptable extends AbstractError(Error) {
constructor(message, data) {
super(message, 'NotAcceptable', 406, 'not-acceptable', data);
}
}

class Timeout extends AbstractError {
class Timeout extends AbstractError(Error) {
constructor(message, data) {
super(message, 'Timeout', 408, 'timeout', data);
}
}

class Conflict extends AbstractError {
class Conflict extends AbstractError(Error) {
constructor(message, data) {
super(message, 'Conflict', 409, 'conflict', data);
}
}

class Unprocessable extends AbstractError {
class Unprocessable extends AbstractError(Error) {
constructor(message, data) {
super(message, 'Unprocessable', 422, 'unprocessable', data);
}
}

class GeneralError extends AbstractError {
class GeneralError extends AbstractError(Error) {
constructor(message, data) {
super(message, 'GeneralError', 500, 'general-error', data);
}
}

class NotImplemented extends AbstractError {
class NotImplemented extends AbstractError(Error) {
constructor(message, data) {
super(message, 'NotImplemented', 501, 'not-implemented', data);
}
}

class Unavailable extends AbstractError {
class Unavailable extends AbstractError(Error) {
constructor(message, data) {
super(message, 'Unavailable', 503, 'unavailable', data);
}
Expand Down
Loading