Code coverage report for models/AjaxCall.js

Statements: 100% (67 / 67)      Branches: 85.29% (29 / 34)      Functions: 100% (22 / 22)      Lines: 100% (37 / 37)      Ignored: 1 statement, 4 branches     

All files » models/ » AjaxCall.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                    1 1 1     1 43 43 43       6         3 3 3 4     3       58       61 61       80       56       49 49       51 51       5 5 5 5       1                     1       2                     2       4       3 3     3       12   12                    
/**
 * AjaxCall class
 *
 * @module AjaxCall
 *
 * @author Luca Pau <luca.pau82@gmail.com>
 */
 
import log from '../log';
 
let _result = Symbol();
let _message = Symbol();
let _value = Symbol();
 
class AjaxCall {
  constructor() {
    this.result = 0;
    this.message = '';
    this.value = {};
  }
 
  static _isEmptyObj(obj) {
    return typeof(obj) === 'object' &&
      Object.getOwnPropertyNames(obj).length === 0;
  }
 
  static _stringifyError(err, filter, space='\n') {
    var plainObject = {};
    Eif(typeof(err) === 'object') {
      Object.getOwnPropertyNames(err).forEach(function(key) {
        plainObject[key] = err[key];
      });
    }
    return JSON.stringify(plainObject, filter, space);
  };
 
  get value() {
    return this[_value];
  }
 
  set result(resultValue) {
    this[_result] = resultValue;
    return this;
  }
 
  get result() {
    return this[_result];
  }
 
  get message() {
    return this[_message];
  }
 
  set value(value) {
    this[_value] = value;
    return this;
  }
 
  set message(messageValue) {
    this[_message] = messageValue;
    return this;
  }
 
  sendErrorMessage(res, resultError, messageError) {
    this.result = resultError;
    this.message = messageError;
    res.status(resultError);
    this.send(res);
  }
 
  sendUncaughtExceptionMessage(res, err) {
    log.fatal(
      `UncaughtException occurred on url:
      ${res.req.url} \n
      with method "${res.req.method}"
      This is the stack trace:\n
      ${err.stack || 'no stack trace found'}\n\n\n
      Original error: \n
      ${err}\n\n
      Original error string formatted:
      ${this.constructor._stringifyError(err)}`
    );
    this.sendErrorMessage(res, 500, 'UncaughtException');
  }
 
  sendUnhandledExceptionMessage(res, err) {
    log.error(
      `UncaughtException occurred on url:
      ${res.req.url} \n
      with method "${res.req.method}"
      This is the stack trace:\n
      ${err.stack || 'no stack trace found'}\n\n\n
      Original error: \n
      ${err}\n\n
      Original error string formatted:
      ${this.constructor._stringifyError(err)}`
    );
    this.sendErrorMessage(res, 501, 'UnhandledException');
  }
 
  send(res) {
    return res.json(this.getStruct());
  }
 
  sendValue(res, valueSent, statusCode=null) {
    this.value = valueSent;
    res.status(
      (statusCode) || (this.constructor._isEmptyObj(this.value) ? 204 : 200)
    );
    this.send(res);
  }
 
  getStruct() {
    var success = this.result === 0;
 
    return {
      code: this.result,
      success: success,
      result: this.result,
      message: this.message,
      value: this.value
    };
  }
}
 
export default AjaxCall;