node-payments
Version:
node-payments is a express based facade to multiple payment services.
319 lines (242 loc) • 7.22 kB
text/coffeescript
# import the external modules
_ = require('lodash')._
extend = require('extend')
colors = require('colors')
config = require('./config')
# # Basic Module
# ### extends [EventEmitter]
# Basic module to handle errors and initialize modules
module.exports = class Basic extends require('events').EventEmitter
# ## internals
# make the deep extend availible for all modles
extend: extend
# **defaults** *Function* basic object to hold config defaults. Will be overwritten by the constructor options
defaults: =>
return config.get( .name.toLowerCase(), true )
###
## constructor
`new Baisc( options )`
Basic constructor. Define the configuration by options and defaults, init logging and init the error handler
{Object} options Basic config object
###
constructor: ( options = {} )->
"_log",
= extend( true, {}, , options )
# init errors
"loaded"
return
###
## initialize
`basic.initialize()`
Overwritible Method to initialize the module
public
###
initialize: =>
return
###
## define
`basic.define( prop, fnGet [, fnSet] )`
Helper to define getter and setter methods fot a property
{ String } prop Property name
{ Function|Object } fnGet Get method or a object with `get` and `set`
{ Function } [fnSet] Set method
public
###
define: ( prop, fnGet, fnSet, writable = true, enumerable = true )=>
_oGetSet =
enumerable: enumerable
writable: writable
if _.isFunction( fnGet )
# set the `defineProperty` object
_oGetSet =
get: fnGet
_oGetSet.set = fnSet if fnSet? and _.isFunction( fnSet )
else
_oGetSet.value = fnGet
# define by object
Object.defineProperty @, prop, _oGetSet
return
###
## getter
`basic.getter( prop, fnGet )`
Shortcut to define a getter
{ String } prop Property name
{ Function } fnGet Get method
public
###
getter: ( prop, _get, enumerable = true )=>
_obj =
enumerable: enumerable
#writable: false
if _.isFunction( _get )
_obj.get = _get
else
_obj.value = _get
Object.defineProperty @, prop, _obj
return
###
## setter
`basic.setter( prop, fnSet )`
Shortcut to define a setter
{ String } prop Property name
{ Function } fnSet Get method
public
###
setter: ( prop, fnGet, enumerable = true )=>
Object.defineProperty @, prop, set: fnGet, enumerable: enumerable, writable: true
return
passEvent: ( obj, ename )=>
return ( args... )->
args.unshift( ename )
obj.emit.apply( obj, args )
return
_waitUntil: ( method, key = "ready", context = @ )=>
return =>
args = arguments
if context[ key ]
method.apply( @, args )
else
context.once key, =>
method.apply( @, args )
return
return
# handle a error
###
## _handleError
`basic._handleError( cb, err [, data] )`
Baisc error handler. It creates a true error object and returns it to the callback, logs it or throws the error hard
{ Function|String } cb Callback function or NAme to send it to the logger as error
{ String|Error|Object } err Error type, Obejct or real error object
private
###
_handleError: ( cb, err, data = {}, errExnd )=>
# try to create a error Object with humanized message
if _.isString( err )
_err = new Error()
_err.name = err
if
_err.message = ?[ err ][ 1 ]?( data ) or "unkown"
else
_err.message = ?[ err ]?( data ) or "unkown"
_err.customError = true
else
_err = err
if errExnd?
_err.data = errExnd
for _k, _v of data
_err[ _k ] = _v
if _.isFunction( cb )
# "error", "", _err
cb( _err )
else if _.isString( cb )
"error", cb, _err
else
throw _err
return _err
###
## log
`base.log( severity, code [, content1, content2, ... ] )`
write a log to the console if the current severity matches the message severity
{ String } severity Message severity
{ String } code Simple code the describe/label the output
{ Any } [contentN] Content to append to the log
public
###
log: ( severity, code, content... )=>
args = [ "_log", severity, code ]
.apply( @, args.concat( content ) )
return
###
## _log
`base._log( severity, code [, content1, content2, ... ] )`
write a log to the console if the current severity matches the message severity
{ String } severity Message severity
{ String } code Simple code the describe/label the output
{ Any } [contentN] Content to append to the log
private
###
_log: ( severity, code, content... )=>
# get the severity and throw a log event
if
_tmpl = "%s %s - #{ new Date().toString()[4..23]} - %s "
args = [ _tmpl, severity.toUpperCase(), .name, code ]
if content.length
args[ 0 ] += "\n"
for _c in content
args.push _c
switch severity
when "fatal"
args[ 0 ] = args[ 0 ].red.bold.inverse
console.error.apply( console, args )
console.trace()
when "error"
args[ 0 ] = args[ 0 ].red.bold
console.error.apply( console, args )
when "warning"
args[ 0 ] = args[ 0 ].yellow.bold
console.warn.apply( console, args )
when "info"
args[ 0 ] = args[ 0 ].blue.bold
console.info.apply( console, args )
when "debug"
args[ 0 ] = args[ 0 ].green.bold
console.log.apply( console, args )
else
return
fatal: ( code, content... )=>
args = [ "_log", "fatal", code ]
.apply( @, args.concat( content ) )
return
error: ( code, content... )=>
args = [ "_log", "error", code ]
.apply( @, args.concat( content ) )
return
warning: ( code, content... )=>
args = ["_log", "warning", code ]
.apply( @, args.concat( content ) )
return
info: ( code, content... )=>
args = [ "_log", "info", code ]
.apply( @, args.concat( content ) )
return
debug: ( code, content... )=>
args = [ "_log", "debug", code ]
.apply( @, args.concat( content ) )
return
###
## _checkLogging
`basic._checkLogging( severity )`
Helper to check if a log will be written to the console
{ String } severity Message severity
{ Boolean } Flag if the severity is allowed to write to the console
private
###
_checkLogging: ( severity )=>
if not ?
= .logging.severitys.indexOf( .logging.severity )
iServ = .logging.severitys.indexOf( severity )
if .logging.severity? and iServ <=
true
else
false
###
## _initErrors
`basic._initErrors( )`
convert error messages to underscore templates
private
###
_initErrors: =>
=
for key, msg of
if
if not _.isFunction( msg[ 1 ] )
[ key ][ 1 ] = _.template( msg[ 1 ] )
else
if not _.isFunction( msg )
[ key ] = _.template( msg )
return
# error message mapping
ERRORS: =>
"ENOTIMPLEMENTED": "This function is planed but currently not implemented"