-
Notifications
You must be signed in to change notification settings - Fork 0
/
exception.js
129 lines (119 loc) · 2.69 KB
/
exception.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
127
128
129
/*jshint node:true, laxcomma: true, smarttabs: true*/
'use strict';
/**
* Loads configuration form different data stores before the server starts
* @module gaz/exception
* @author Eric Satterwhite
* @since 0.1.0
* @requires class
* @requires options
*/
var util = require('util')
, Class = require( './class' ) //
, Options = require( './class/options' ) //
, Parent = require( './class/parent' ) //
, Exception
;
/**
* A subclassible Error class that retains Name, message and stack when subclassed
* @constructor
* @alias module:gaz/exception
* @extends Error
* @mixes module:gaz/class/options
* @param {Object} options Config options for the error instance
* @example var Exception = require("gaz/exception");
var FakeExcpetion = new Class({
inherits:exception
,options:{
name:"FakeExcpetion"
}
});
var e = new FakeException({
message:"Unable to read file",
code:2100
type:'unreadable_file'
});
console.log( e instanceof Error ); // true
console.log( e instanceof exception ); // true
console.log( e instanceof FakeExcpetion ); // true
throw e
*/
module.exports = Exception = new Class(/* @lends module:gaz/exception.prototype */{
inherits: Error
,mixin: [Parent, Options]
,options:{
name:'Exception'
,message:""
,code:1000
,type:'base_exception'
}
,constructor: function( options ){
var tmp // temporary error object
this.setOptions( options );
// force this thing to really be an error
tmp = Error.call( this, this.options.message );
tmp.name = this.name;
this.stack = tmp.stack;
// cleanup
tmp = undefined;
}
,toString: function toString(){
return util.format( '[%s] %s', this.name, this.message )
}
,toJSON: function(){
return {
name:this.name
,message: this.message
,code: this.code
,stack: this.stack
,type: this.type
}
}
});
Object.defineProperties( Exception.prototype,{
/**
* @readonly
* @static
* @name name
* @memberof module:gaz/exception
* @property {String} name The name of the exception
*/
name:{
get: function(){
return this.options.name;
}
}
/**
* @readonly
* @name code
* @memberof module:gaz/exception
* @property {Number} Internal numberic representation of the error
*/
, code:{
get: function(){
return this.options.code;
}
}
/**
* @readonly
* @name type
* @memberof module:gaz/exception
* @property {String} type The internal type of the error
*/
, type:{
get: function(){
return this.options.type;
}
}
/**
* @readonly
* @name message
* @memberof module:gaz/exception
* @property {String} message The message the error will display when thrown
*/
, message:{
get: function(){
return this.options.message;
}
}
})