all files / couch/util/ error-base.js

65.52% Statements 19/29
42.86% Branches 6/14
60% Functions 3/5
67.86% Lines 19/28
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            247× 247×     247× 247×             247× 247×       13×   13×         13×       13×       13×                                    
import Ember from 'ember';
 
const {
  merge,
  A
} = Ember;
 
function message(opts) {
  opts = opts || {};
  Iif(opts.status) {
    return `${opts.status} ${opts.error}: ${opts.reason}`;
  } else {
    Eif(opts.error || opts.reason) {
      return `${opts.error}: ${opts.reason}`;
    } else {
      return 'unknown couch error';
    }
  }
}
 
export function BaseError(opts) {
  Ember.Error.call(this, message(opts));
  merge(this, opts);
}
 
BaseError.prototype = Object.create(Ember.Error.prototype);
 
BaseError.prototype.toJSON = function() {
  let { status, error, reason, id } = this;
 
  let hash = {
    error,
    reason
  };
 
  Iif(status) {
    hash.status = status;
  }
 
  Iif(id) {
    hash.id = id;
  }
 
  return hash;
};
 
export function BaseErrors(array) {
  Ember.Error.call(this, 'Multiple errors');
  this.errors = array;
}
 
BaseErrors.prototype = Object.create(Ember.Error.prototype);
 
BaseErrors.prototype.toJSON = function() {
  let { message, errors } = this;
 
  errors = A(errors).map(error => error.toJSON ? error.toJSON() : error);
 
  return {
    message,
    errors
  };
};