Skip to content

Commit

Permalink
BatchError refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Aug 27, 2019
1 parent 801c3ca commit 8f3b98a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 49 deletions.
102 changes: 54 additions & 48 deletions lib/errors/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const npm = {
};

/**
* @interface errors.BatchError
* @class errors.BatchError
* @augments external:Error
* @description
* This type represents all errors rejected by method {@link batch}, except for {@link external:TypeError TypeError}
Expand Down Expand Up @@ -54,66 +54,72 @@ const npm = {
* @see {@link batch}
*
*/
function BatchError(result, errors, duration) {
class BatchError extends Error {

// TODO: Replace with ES6 class (like in pg-promise)
constructor(result, errors, duration) {

this.data = result;

/**
* @method errors.BatchError.getErrors
* @description
* Returns the complete list of errors only.
*
* It supports nested batch results, presented as a sub-array.
*
* @returns {array}
*/
this.getErrors = function () {
const err = new Array(errors.length);
for (let i = 0; i < errors.length; i++) {
err[i] = result[errors[i]].result;
if (err[i] instanceof BatchError) {
err[i] = err[i].getErrors();
function getErrors() {
const err = new Array(errors.length);
for (let i = 0; i < errors.length; i++) {
err[i] = result[errors[i]].result;
if (err[i] instanceof BatchError) {
err[i] = err[i].getErrors();
}
}
npm.utils.extend(err, '$isErrorList', true);
return err;
}
npm.utils.extend(err, '$isErrorList', true);
return err;
};

const e = this.getErrors();
let first = e[0];
const e = getErrors();

while (first && first.$isErrorList) {
first = first[0];
}
let first = e[0];

// we do not show it within the inspect, because when the error
// happens for a nested result, the output becomes a mess.
this.first = first;
while (first && first.$isErrorList) {
first = first[0];
}

if (first instanceof Error) {
this.message = first.message;
} else {
if (typeof first !== 'string') {
first = npm.u.inspect(first);
let message;

if (first instanceof Error) {
message = first.message;
} else {
if (typeof first !== 'string') {
first = npm.u.inspect(first);
}
message = first;
}
this.message = first;
}

this.stat = {
total: result.length,
succeeded: result.length - e.length,
failed: e.length,
duration: duration
};
super(message);
this.name = this.constructor.name;

Error.captureStackTrace(this, BatchError);
this.data = result;

}
// we do not show it within the inspect, because when the error
// happens for a nested result, the output becomes a mess.
this.first = first;

this.stat = {
total: result.length,
succeeded: result.length - e.length,
failed: e.length,
duration: duration
};

this.getErrors = getErrors;

Error.captureStackTrace(this, this.constructor);
}

npm.u.inherits(BatchError, Error);
BatchError.prototype.name = 'BatchError';
/**
* @method errors.BatchError.getErrors
* @description
* Returns the complete list of errors only.
*
* It supports nested batch results, presented as a sub-array.
*
* @returns {array}
*/
}

/**
* @method errors.BatchError.toString
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function extend(obj, name, value) {
///////////////////////////////////////////
// Returns a space gap for console output;
function messageGap(level) {
return Array(1 + level * 4).join(' ');
return ' '.repeat(level * 4);
}

function formatError(error, level) {
Expand Down
2 changes: 2 additions & 0 deletions test/ext/page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const spex = lib.main(promise);

const isError = lib.isError;
const PageError = require('../../lib/errors/page');
const BatchError = require('../../lib/errors/batch');

describe('Page - negative', () => {

Expand Down Expand Up @@ -245,6 +246,7 @@ describe('Page - negative', () => {
it('must reject correctly', () => {
expect(error.index).toBe(2);
expect(error.error.name).toBe('BatchError');
expect(error.error instanceof BatchError).toBeTruthy();
expect(error.error.data).toEqual([
{
success: true,
Expand Down

0 comments on commit 8f3b98a

Please sign in to comment.