Skip to content

Instantly share code, notes, and snippets.

@artlogic
Created December 15, 2016 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artlogic/28026869470ac36e2fed4efc55d3e357 to your computer and use it in GitHub Desktop.
Save artlogic/28026869470ac36e2fed4efc55d3e357 to your computer and use it in GitHub Desktop.
Possible testdouble.js regression?
'use strict';
// Much of this was inspired by sequelize's error module
// https://github.com/sequelize/sequelize/blob/master/lib/errors.js
var util = require('util');
var error = {};
// Every built in error inherits from this error
error.BaseError = function () {
var tmp = Error.apply(this, arguments);
tmp.name = this.name = 'BaseError';
this.message = tmp.message;
this.status = 500;
Error.captureStackTrace(this, this.constructor);
};
util.inherits(error.BaseError, Error);
error.BaseError.prototype.toJSON = function () {
return {
type: this.name,
message: this.message
};
};
// used when the user cannot access something
error.AccessDeniedError = function (message) {
error.BaseError.call(this, message);
this.name = 'AccessDeniedError';
this.status = 403;
};
util.inherits(error.AccessDeniedError, error.BaseError);
// Used when the user passes in invalid data
error.ValidationError = function (message, validations) {
error.BaseError.call(this, message);
this.name = 'ValidationError';
this.status = 422;
if (typeof validations === 'undefined') {
validations = null;
}
this.validations = validations;
};
util.inherits(error.ValidationError, error.BaseError);
error.ValidationError.prototype.toJSON = function () {
var json = error.BaseError.prototype.toJSON.call(this);
json.validations = this.validations;
return json;
};
module.exports = error;
'use strict';
var td = require('testdouble');
var error = td.replace('./error');
var ad = new error.AccessDeniedError('Whoops!'); // works in 1.9.1 and 1.10.0
// for me, in 1.10.0 this returns `error.ValidationError is not a constructor`
var vd = new error.ValidationError('Oh no!'); // only works in 1.9.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment