mailtrain/shared/interoperable-errors.js

92 lines
2.1 KiB
JavaScript
Raw Normal View History

'use strict';
class InteroperableError extends Error {
constructor(type, msg, data) {
super(msg);
this.type = type;
this.data = data;
}
}
class NotLoggedInError extends InteroperableError {
constructor(msg, data) {
super('NotLoggedInError', msg, data);
}
}
class ChangedError extends InteroperableError {
constructor(msg, data) {
super('ChangedError', msg, data);
}
}
class NotFoundError extends InteroperableError {
constructor(msg, data) {
super('NotFoundError', msg, data);
}
}
class LoopDetectedError extends InteroperableError {
constructor(msg, data) {
super('LoopDetectedError', msg, data);
}
}
2017-06-08 22:23:03 +00:00
class ChildDetectedError extends InteroperableError {
constructor(msg, data) {
super('ChildDetectedError', msg, data);
}
}
class DuplicitNameError extends InteroperableError {
constructor(msg, data) {
super('DuplicitNameError', msg, data);
}
}
2017-06-08 22:23:03 +00:00
class DuplicitEmailError extends InteroperableError {
constructor(msg, data) {
super('DuplicitEmailError', msg, data);
}
}
class IncorrectPasswordError extends InteroperableError {
constructor(msg, data) {
super('IncorrectPasswordError', msg, data);
}
}
class InvalidToken extends InteroperableError {
constructor(msg, data) {
super('InvalidToken', msg, data);
}
}
const errorTypes = {
InteroperableError,
NotLoggedInError,
ChangedError,
NotFoundError,
2017-06-08 22:23:03 +00:00
LoopDetectedError,
ChildDetectedError,
DuplicitNameError,
DuplicitEmailError,
IncorrectPasswordError,
InvalidToken
};
function deserialize(errorObj) {
if (errorObj.type) {
const ctor = errorTypes[errorObj.type];
if (ctor) {
return new ctor(errorObj.message, errorObj.data);
} else {
console.log('Warning unknown type of interoperable error: ' + errorObj.type);
}
}
}
module.exports = Object.assign({}, errorTypes, {
deserialize
});