cbp-lib
Version:
Libraries for cbp
87 lines (73 loc) • 1.82 kB
JavaScript
'use strict'
export class BaseError extends Error {
constructor(error) {
super(error)
this.name = 'BaseError'
this.message = error
this.stackTrace = this.stackTrace
}
toString() {
return {
message: this.message,
stackTrace: this.stackTrace
}
}
toJson() {
return {
name: this.name,
message: this.message,
stackTrace: this.stackTrace
}
}
}
export class ArgumentError extends BaseError {
constructor(error) {
super(error)
this.name = 'ArgumentError'
this.message = error
}
}
export class PermissionError extends BaseError {
constructor(status=401, error) {
super(error)
this.name = 'PermissionError'
this.status = status
this.message = error
}
toJson() {
return {
name: this.name,
status: this.status,
message: this.message
}
}
}
export class Validation {
static isEmpty(value) {
if (typeof value === 'object') return Validation.isEmptyObject(value)
if(!value || value === 'undefined' || value == '' || value == ' ' || value === null) {
return true
}
else {
return false
}
}
static isEmptyObject(obj) {
for(let key in obj) {
if(obj.hasOwnProperty(key))
return false;
}
return true;
}
static removeNullKey(obj) {
let newObj = Object.assign({}, obj)
for(let key in newObj) {
if(newObj.hasOwnProperty(key)) {
if (this.isEmpty(newObj[key])) {
delete newObj[key]
}
}
}
return newObj
}
}