Code coverage report for src/Common/Exception/ExceptionList.js

Statements: 95.24% (40 / 42)      Branches: 90% (9 / 10)      Functions: 90.91% (10 / 11)      Lines: 95.12% (39 / 41)      Ignored: none     

All files » src/Common/Exception/ » ExceptionList.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181    1                       1   434         434   434                   1     434     434       434               434         13           434         52           434         515                             434   88     88 33       88     88         91 4     91 1     90                         434 3                       434   65       65       71 71             71 6         65           1 1     1  
"use strict";
 
var _               = require("lodash"),
    Type            = require("../Type"),
    Safe            = require("../Safe"),
    Exception       = require("./Exception");
 
/**
 *
 * Get the stack trace
 *
 * @return {String}
 *
 */
var getStackTrace = function(){
 
    var error    = new Error(),
        stackStr = Safe.string(error.stack),
        stack    = stackStr.split('\n');
 
    /// Remove Error message + 2 first lines of the stack trace
    stack.splice(0,3);
 
    return stack.join('\n');
 
};
 
/**
 *
 * Error class that can contain multiple errors
 * @class
 *
 */
var ExceptionList = function() {
 
    /// instance of error
    var items  = [];
 
    /// get the stack trace
    var stack = getStackTrace();
 
    /// define the this.stack to return the created Error
    /// stack
    Object.defineProperty(
        this,
        'stack', {
        get: function() {
            return this.toString() + "\n" + stack;
        }
    });
 
    Object.defineProperty(
        this,
        "message",
        {
            get: function(){
                return this.toString();
            },
            configurable: false
        }
    );
 
    Object.defineProperty(
        this,
        "items",
        {
            get: function(){
                return _.clone(items);
            },
            configurable: false
        }
    );
 
    Object.defineProperty(
        this,
        "length",
        {
            get: function(){
                return items.length;
            },
            configurable: false
        }
    );
 
    /**
     *
     * Adds an element to the error list
     *
     * @throws {Error} If invalid argument
     *
     * @param {*} errors
     *
     */
    this.push = function(errors){
 
        var scope = this;
 
        /// if argument is an ExceptionList
        if(errors instanceof ExceptionList){
            errors = errors.items;
        }
 
        /// make sure errors is an array
        errors = Safe.array(errors);
 
        /// adds the elements to the list
        _.each(
            errors,
            function(error){
 
                /// if error is on string format add it as an Exception
                if( Type.isString(error) ){
                    error = new Exception(error);
                }
 
                if( !(error instanceof Error) ){
                    throw new Error("Expected instance of type Error");
                }
 
                items.push(error);
 
            });
 
    };
 
    /**
     *
     * Clear the error list
     *
     * @return
     *
     */
    this.clear = function(){
        while(items.length) items.pop();
    };
 
    /**
     *
     * ToString
     *
     * @param {Object} translations
     *
     * @return {String}
     *
     */
    this.toString = function(translations){
 
        var str         = "",
            length      = items.length,
            separator   = ", ";
 
        _.each(
            items,
            function(error, index){
 
                Eif(error instanceof Exception){
                    str += error.toString(translations);
                }
                else {
                    str += error.toString();
                }
 
                /// add the separator if its not the last element
                if(index != length-1){
                    str += separator;
                }
 
            });
 
        return str;
 
    };
 
};
 
ExceptionList.prototype = new Error();
ExceptionList.prototype.constructor = ExceptionList;
 
 
module.exports = ExceptionList;