¶ MPBasic¶ extends EventEmitter¶ Exports: ConstructorThis is a general basic class. It inculdes some methods that are often usefull. It integrates logging, error handling getter/setter, ... Example:
new FooClass() // -> DEBUG FooClass - Nov 07 2014 15:42:14 - init |
|
npm modules |
|
¶ Basic Module¶ extends [EventEmitter]Basic module to handle errors and initialize modules |
|
¶ internals |
|
make the deep extend availible for all modles |
|
defaults Function basic object to hold config defaults. Will be overwritten by the constructor options |
|
¶ constructor
Basic constructor. Define the configuration by options and defaults, init logging and init the error handler Params
options
Object
Basic config object
|
|
init errors |
|
useage: |
|
¶ mixin
Method to include other class methods to this class. Params
mixins...
Class
One or more classes as arguments
API
public
Example:
|
|
¶ initialize
Overwritible Method to initialize the module Params
options
Object
Basic config object passed to constructor
API
public
|
|
¶ define
Helper to define getter and setter methods fot a property Params
prop
String
Property name
fnGet
Function
Object
Get method or a object with
get and set
[fnSet]
Function
Set method
API
public
|
|
set the |
|
define by object |
|
¶ getter
Shortcut to define a getter Params
prop
String
Property name
fnGet
Function
Get method
API
public
|
|
writable: false |
|
¶ setter
Shortcut to define a setter Params
prop
String
Property name
fnSet
Function
Get method
API
public
|
|
¶ _waitUntil
Wrapper method to create a methos thas is only called until the Params
method
Function
The function to call.
[
String
key="ready" ] the key to listen for.
[context={self}]
Context
The context to lsiten to the key. Per default it is the instance it self
@ or this .
API
public
|
|
handle a error ¶ _handleError
Baisc error handler. It creates a true error object and returns it to the callback, logs it or throws the error hard Params
cb
Function
String
Callback function or NAme to send it to the logger as error
err
String
Error
Object
Error type, Obejct or real error object
API
private
|
|
try to create a error Object with humanized message |
233
234 if _isString( err )
235 _err = new Error()
236 _err.name = err
237 _err.message = @_ERRORS?[ err ][ 1 ]?( data ) or "unkown"
238 _err.statusCode = @_ERRORS?[ err ][ 0 ] or 500
239 _err.customError = true
240 else
241 _err = err
242
243 if errExnd?
244 _err.data = errExnd
245
246 for _k, _v of data
247 _err[ _k ] = _v
248
249 if _isFunction( cb )
250
251 |
@log "error", "", _err |
|
¶ log
write a log to the console if the current severity matches the message severity Params
severity
String
Message severity
code
String
Simple code the describe/label the output
[contentN]
Any
Content to append to the log
API
public
|
|
¶ _log
write a log to the console if the current severity matches the message severity Params
severity
String
Message severity
code
String
Simple code the describe/label the output
[contentN]
Any
Content to append to the log
API
private
|
|
get the severity and throw a log event |
292
293
294 if @_checkLogging( severity )
295 _tmpl = "%s %s - #{ new Date().toString()[4..23]} - %s "
296
297 args = [ _tmpl, severity.toUpperCase(), @_logname(), code ]
298
299 if content.length
300 args[ 0 ] += "\n"
301 for _c in content
302 args.push _c
303
304 switch severity
305 when "fatal"
306 args[ 0 ] = args[ 0 ].red.bold.inverse
307 console.error.apply( console, args )
308 for arg in args when arg instanceof Error
309 console.log arg.stack
310 return
311 console.trace()
312 when "error"
313 args[ 0 ] = args[ 0 ].red.bold
314 console.error.apply( console, args )
315 when "warning"
316 args[ 0 ] = args[ 0 ].yellow.bold
317 console.warn.apply( console, args )
318 when "info"
319 args[ 0 ] = args[ 0 ].blue.bold
320 console.info.apply( console, args )
321 when "debug"
322 args[ 0 ] = args[ 0 ].green.bold
323 console.log.apply( console, args )
324 else
325
326 return
327
328
329 |
¶ _logname
Helper method to overwrite the name displayed withing the console output Params
String
Desc
Function
Callback function
Returns
String
Return Desc
API
private
|
|
¶ fatal
Shorthand to output a fatal log Params
code
String
Simple code the describe/label the output
[contentN]
Any
Content to append to the log
API
public
|
|
¶ error
Shorthand to output a error log Params
code
String
Simple code the describe/label the output
[contentN]
Any
Content to append to the log
API
public
|
|
¶ warning
Shorthand to output a warning log Params
code
String
Simple code the describe/label the output
[contentN]
Any
Content to append to the log
API
public
|
|
¶ info
Shorthand to output a info log Params
code
String
Simple code the describe/label the output
[contentN]
Any
Content to append to the log
API
public
|
|
¶ debug
Shorthand to output a debug log Params
code
String
Simple code the describe/label the output
[contentN]
Any
Content to append to the log
API
public
|
|
¶ _checkLogging
Helper to check if a log will be written to the console Params
severity
String
Message severity
Returns
Boolean
Flag if the severity is allowed to write to the console
API
private
|
442
443 _checkLogging: ( severity )=>
444 if not @_logging_iseverity?
445 @_logging_iseverity = @config.logging.severitys.indexOf( @config.logging.severity )
446
447 iServ = @config.logging.severitys.indexOf( severity )
448 if @config.logging.severity? and iServ <= @_logging_iseverity
449 true
450 else
451 false
452
453
454 |
¶ ERRORS
Error detail mappings Returns
Object
Return A Object of error details. Format:
"ERRORCODE":[ ststudCode, "Error detail" ]
API
private
|