Skip to content

Sync errors and error management #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions lib/errors/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

exports.ErrorManager = require('./error-manager');

exports.SfPersistanceError = require('./sf-persistance-error');
exports.SyncModulePullError = require('./sync-module-pull-error');
exports.Validation = require('./validation');
module.exports = {
ErrorManager: require('./error-manager'),
SyncError: require('./error-manager'),
...require('./validation'),
...require('./operational')
};
5 changes: 5 additions & 0 deletions lib/errors/operational/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.OperationalError = require('./operational-error');
exports.SfPersistanceError = require('./sf-persistance-error');
exports.SyncModulePullError = require('./sync-module-pull-error');
5 changes: 5 additions & 0 deletions lib/errors/operational/operational-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

const SyncError = require('../sync-error');

module.exports = class OperationalError extends SyncError {};
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

const find = require('lodash/find');
const matchAll = require('match-all');
const OperationalError = require('./operational-error');

class SfPersistanceError {
class SfPersistanceError extends OperationalError {
constructor(original, Model, entries) {
this.name = 'SfPersistanceError';
super(`Unable to persist fetched ${Model.name} salesforce entries`);
this.original = original;
this.Model = Model;
this.entries = entries;
this.message = `Unable to persist fetched ${Model.name} salesforce entries`;

if (!this[original.name]) throw original;
this.failingField = this[original.name]();
Expand Down
13 changes: 13 additions & 0 deletions lib/errors/operational/sync-module-pull-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const OperationalError = require('./operational-error');

class SyncModulePullError extends OperationalError {
constructor(original, SyncModule) {
super(`Error pulling ${SyncModule.Model.name} salesforce records`);
this.original = original;
this.SyncModule = SyncModule;
}
}

module.exports = SyncModulePullError;
12 changes: 12 additions & 0 deletions lib/errors/sync-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

class SyncError extends Error {
constructor(message) {
super(message);
this.createdAt = Date.now();
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
}
}

module.exports = SyncError;
12 changes: 0 additions & 12 deletions lib/errors/sync-module-pull-error.js

This file was deleted.

14 changes: 5 additions & 9 deletions lib/errors/validation/attribute-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

const ValidationError = require('./validation-error');

class AttributeError extends ValidationError {
class AttributeValidationError extends ValidationError {
constructor(originalError) {
super(originalError.instance);
this.name = 'AttributeError';
this.original = originalError;
}

get message() {
return `${this.name}: ${this.original.message}; ${this.instanceInfo}`;
const { instance, message } = originalError;
super(instance, message);
this.originalError = originalError;
}
}

module.exports = AttributeError;
module.exports = AttributeValidationError;
15 changes: 7 additions & 8 deletions lib/errors/validation/consumable-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

const ValidationError = require('./validation-error');

class ConsumableError extends ValidationError {
class ConsumableValidationError extends ValidationError {
constructor(instance, association) {
super(...arguments);
this.name = 'ConsumableError';
super(instance, getMessage(association));
this.association = association;
}

get message() {
return `${this.name}: Invalid consumable association: ${this.association.associationAccessor}; ${this.instanceInfo}`;
}
}

module.exports = ConsumableError;
module.exports = ConsumableValidationError;

function getMessage(association) {
return `Invalid consumable association: ${association.associationAccessor}`;
}
15 changes: 5 additions & 10 deletions lib/errors/validation/domain-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@

const ValidationError = require('./validation-error');

class DomainError extends ValidationError {
constructor(instance, originalMessage) {
super(...arguments);
this.name = 'DomainError';
this.originalMessage = originalMessage;
}

get message() {
return `${this.name}: ${this.originalMessage}; ${this.instanceInfo}`;
class DomainValidationError extends ValidationError {
constructor(instance, message) {
super(instance, message);
this.originalMessage = message;
}
}

module.exports = DomainError;
module.exports = DomainValidationError;
24 changes: 11 additions & 13 deletions lib/errors/validation/fk-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@

const ValidationError = require('./validation-error');

class ValidationFkError extends ValidationError {
class ForeignKeyValidationError extends ValidationError {
constructor(instance, association) {
super(...arguments);
this.name = 'ValidationFkError';
this.alias = association.associationAccessor;
this.fk = association.identifier;
const alias = association.associationAccessor;
const fk = association.identifier;
const value = instance[fk];
const message = getMessage(alias, fk, value);
super(instance, message);
Object.assign(this, { alias, fk, value });
}
}

get message() {
return `${this.name}: No ${this.alias} found for ${this.fk}: ${this.value}; ${this.instanceInfo}`;
}
module.exports = ForeignKeyValidationError;

get value() {
return this.instance[this.fk];
}
function getMessage(alias, fk, value) {
return `No ${alias} found for ${fk}: ${value}`;
}

module.exports = ValidationFkError;
22 changes: 11 additions & 11 deletions lib/errors/validation/uniqueness-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ const map = require('lodash/map');
const transform = require('lodash/transform');
const ValidationError = require('./validation-error');

class ValidationUniquenessError extends ValidationError {
constructor(instance, index) {
super(...arguments);
this.name = 'ValidationUniquenessError';
this.index = index;
this.values = parseValues(instance, index);
}

get message() {
return `${this.name}: duplicate value for ${this.index.join()}: ${map(this.values).join()}; ${this.instanceInfo}`;
class UniquenessValidationError extends ValidationError {
constructor(instance, index = []) {
const values = parseValues(instance, index);
const message = getMessage(index, values);
super(instance, message);
Object.assign(this, { values, index });
}
}

module.exports = ValidationUniquenessError;
module.exports = UniquenessValidationError;

function parseValues(instance, index) {
return transform(index, (acc, it) => {
const attribute = find(instance.rawAttributes, { field: it }).fieldName;
acc[it] = instance[attribute];
}, {});
}

function getMessage(index, values) {
return `Duplicate value for ${index.join()}: ${map(values).join()}`;
}
27 changes: 12 additions & 15 deletions lib/errors/validation/validation-error.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
'use strict';

class ValidationError {
constructor(instance) {
this.name = 'ValidationError';
this.instance = instance;
}

get id() {
return this.instance.id;
}
const SyncError = require('../sync-error');

get modelName() {
return this.instance.constructor.name;
}

get instanceInfo() {
return `${this.modelName} id: ${this.id}`;
class ValidationError extends SyncError {
constructor(instance, message = '') {
const modelName = instance.constructor.name;
const instanceId = instance.id;
super(getMessage(message, modelName, instanceId));
Object.assign(this, { instance, instanceId, modelName });
}
}

module.exports = ValidationError;

function getMessage(message, modelName, id) {
const instanceInfo = `${modelName} id: ${id}`;
return [message, instanceInfo].filter(Boolean).join('; ');
}
2 changes: 1 addition & 1 deletion lib/sync/sync-module-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const errors = require('../errors/validation');
const errors = require('../errors');
const filter = require('lodash/filter');
const get = require('lodash/get');
const map = require('lodash/map');
Expand Down
4 changes: 1 addition & 3 deletions lib/sync/sync-module.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const errors = require('../errors');
const { ErrorManager, SfPersistanceError, SyncModulePullError } = require('../errors');
const filter = require('lodash/filter');
const invokeMap = require('lodash/invokeMap');
const Loader = require('../utils/loader');
Expand All @@ -9,8 +9,6 @@ const partition = require('lodash/partition');
const Promise = require('bluebird');
const SyncModuleValidator = require('./sync-module-validator');

const { ErrorManager, SfPersistanceError, SyncModulePullError } = errors;

class SyncModule {
constructor({ Model, sync }) {
this.Model = Model;
Expand Down