great-scott
Version:
A Marty-like data source for postgres.
179 lines (144 loc) • 5.46 kB
JavaScript
;
var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); };
var _inherits = function (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) subClass.__proto__ = superClass; };
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
var DUP_KEY = /^duplicate key value violates unique constraint/i;
var NOT_NULL = /^null value in column ".*" violates not-null constraint$/i;
var FOREIGN_KEY = /^insert or update on table ".*" violates foreign key constraint/;
var NO_RELATION = /^relation ".*" does not exist$/i;
var PostgresError = (function (Error) {
function PostgresError(message, code) {
_classCallCheck(this, PostgresError);
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
this.code = code;
}
_inherits(PostgresError, Error);
_prototypeProperties(PostgresError, {
factory: {
value: function factory(err) {
if (err.message.match(DUP_KEY)) {
return UniqueConstraintError.fromError(err);
} else if (err.message.match(NOT_NULL)) {
return NotNullConstraintError.fromError(err);
} else if (err.message.match(FOREIGN_KEY)) {
return ForeignKeyConstraintError.fromError(err);
} else if (err.message.match(NO_RELATION)) {
return RelationNotFoundError.fromError(err);
}
return err;
},
writable: true,
configurable: true
}
});
return PostgresError;
})(Error);
var ForeignKeyConstraintError = (function (PostgresError) {
function ForeignKeyConstraintError() {
_classCallCheck(this, ForeignKeyConstraintError);
if (PostgresError != null) {
PostgresError.apply(this, arguments);
}
}
_inherits(ForeignKeyConstraintError, PostgresError);
_prototypeProperties(ForeignKeyConstraintError, {
fromError: {
value: function fromError(err) {
var newErr = new ForeignKeyConstraintError(err.message, err.code);
newErr.detail = err.detail;
newErr.table = err.table;
newErr.column = err.column;
newErr.constraint = err.constraint;
return newErr;
},
writable: true,
configurable: true
}
});
return ForeignKeyConstraintError;
})(PostgresError);
var NotNullConstraintError = (function (PostgresError) {
function NotNullConstraintError() {
_classCallCheck(this, NotNullConstraintError);
if (PostgresError != null) {
PostgresError.apply(this, arguments);
}
}
_inherits(NotNullConstraintError, PostgresError);
_prototypeProperties(NotNullConstraintError, {
fromError: {
value: function fromError(err) {
var newErr = new NotNullConstraintError(err.message, err.code);
newErr.detail = err.detail;
newErr.table = err.table;
newErr.column = err.column;
return newErr;
},
writable: true,
configurable: true
}
});
return NotNullConstraintError;
})(PostgresError);
var UniqueConstraintError = (function (PostgresError) {
function UniqueConstraintError() {
_classCallCheck(this, UniqueConstraintError);
if (PostgresError != null) {
PostgresError.apply(this, arguments);
}
}
_inherits(UniqueConstraintError, PostgresError);
_prototypeProperties(UniqueConstraintError, {
fromError: {
value: function fromError(err) {
var newErr = new UniqueConstraintError(err.message, err.code);
newErr.detail = err.detail;
newErr.table = err.table;
newErr.constraint = err.constraint;
return newErr;
},
writable: true,
configurable: true
}
});
return UniqueConstraintError;
})(PostgresError);
var RelationNotFoundError = (function (PostgresError) {
function RelationNotFoundError() {
_classCallCheck(this, RelationNotFoundError);
if (PostgresError != null) {
PostgresError.apply(this, arguments);
}
}
_inherits(RelationNotFoundError, PostgresError);
_prototypeProperties(RelationNotFoundError, {
fromError: {
value: function fromError(err) {
return new RelationNotFoundError(err.message, err.code);
},
writable: true,
configurable: true
}
});
return RelationNotFoundError;
})(PostgresError);
var NotFoundError = (function (PostgresError) {
function NotFoundError() {
_classCallCheck(this, NotFoundError);
if (PostgresError != null) {
PostgresError.apply(this, arguments);
}
}
_inherits(NotFoundError, PostgresError);
return NotFoundError;
})(PostgresError);
module.exports = {
PostgresError: PostgresError,
ForeignKeyConstraintError: ForeignKeyConstraintError,
NotNullConstraintError: NotNullConstraintError,
UniqueConstraintError: UniqueConstraintError,
NotFoundError: NotFoundError,
RelationNotFoundError: RelationNotFoundError };
/* jshint latedef:false */