This is how we can grant priviledges to remote js without needing signed code, or...
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / evergreen / util / error.js
1 dump('entering util/error.js\n');
2
3 if (typeof util == 'undefined') util = {};
4 util.error = function () {
5
6         try {
7                 this.consoleService = Components.classes['@mozilla.org/consoleservice;1']
8                         .getService(Components.interfaces.nsIConsoleService);
9         } catch(E) {
10                 this.consoleDump = false;
11                 dump('util.error constructor: ' + E + '\n');
12         }
13
14         this.sdump_last_time = new Date();
15
16         return this;
17 };
18
19 util.error.prototype = {
20
21         'printDebug' : true,
22         'consoleDump' : true,
23         'debugDump' : true,
24         'arg_dump_full' : false,
25
26         'debug' : function(e){
27                 dump('-----------------------------------------\n' 
28                         + e + '\n-----------------------------------------\n' );
29         },
30
31         'sdump_levels' : {
32
33                 'D_NONE' : false, 'D_ALL' : true, 'D_ERROR' : true, 'D_DEBUG' : true, 'D_TRACE' :  false,
34                 'D_TRACE_ENTER' :  false, 'D_TRACE_EXIT' :  false, 'D_TIMEOUT' :  false, 'D_FILTER' : false,
35                 'D_CONSTRUCTOR' : false, 'D_FIREFOX' : false, 'D_LEGACY' : false,
36
37                 'D_CLAM' : false, 'D_PAGED_TREE' : false, 'D_GRID_LIST' : false, 'D_HTML_TABLE' : false,
38                 'D_TAB' : false,
39
40                 'D_AUTH' : true, 'D_OPAC' : true, 'D_CAT' : false,
41
42                 'D_PATRON_SEARCH' : false, 'D_PATRON_SEARCH_FORM' : false, 'D_PATRON_SEARCH_RESULTS' : false,
43
44                 'D_PATRON_DISPLAY' : false, 'D_PATRON_DISPLAY_STATUS' : false, 'D_PATRON_DISPLAY_CONTACT' : false,
45
46                 'D_PATRON_ITEMS' : false, 'D_PATRON_CHECKOUT_ITEMS' : false, 'D_PATRON_HOLDS' : false,
47                 'D_PATRON_BILLS' : false, 'D_PATRON_EDIT' : false,
48
49                 'D_CHECKIN' : false, 'D_CHECKIN_ITEMS' : false,
50
51                 'D_HOLD_CAPTURE' : false, 'D_HOLD_CAPTURE_ITEMS' : false,
52
53                 'D_PATRON_UTILS' : false, 'D_CIRC_UTILS' : false,
54
55                 'D_FILE' : true, 'D_EXPLODE' : false, 'D_FM_UTILS' : false, 'D_PRINT' : false, 'D_SES' : true,
56                 'D_SES_FUNC' : false, 'D_SES_RESULT' : true, 'D_SPAWN' : false, 'D_STRING' : false,
57                 'D_UTIL' : false, 'D_WIN' : true, 'D_WIDGETS' : false
58         },
59
60         'filter_console_init' : function (p) {
61                 this.sdump('D_FILTER',this.arg_dump(arguments,{0:true}));
62
63                 var filterConsoleListener = {
64                         observe: function( msg ) {
65                                 try {
66                                         p.observe_msg( msg );
67                                 } catch(E) {
68                                         alert(E);
69                                 }
70                         },
71                         QueryInterface: function (iid) {
72                                 if (!iid.equals(Components.interfaces.nsIConsoleListener) &&
73                                         !iid.equals(Components.interfaces.nsISupports)) {
74                                                 throw Components.results.NS_ERROR_NO_INTERFACE;
75                                 }
76                                 return this;
77                         }
78                 };
79                 try {
80                         this.consoleService.registerListener(filterConsoleListener);    
81                 } catch(E) {
82                         alert(E);
83                 }
84
85                 this.sdump('D_TRACE_EXIT',this.arg_dump(arguments));
86         },
87
88         'sdump' : function (level,msg) {
89                 try {
90                         var now = new Date();
91                         var message = now.valueOf() + '\tdelta = ' + (now.valueOf() - this.sdump_last_time.valueOf()) + '\t' + level + '\n' + msg;
92                         if (this.sdump_levels['D_NONE']) return null;
93                         if (this.sdump_levels[level]||this.sdump_levels['D_ALL']) {
94                                 this.sdump_last_time = now;
95                                 if (this.debugDump)
96                                         this.debug(message);
97                                 if (this.consoleDump)
98                                         this.consoleService.logStringMessage(message);
99                         }
100                 } catch(E) {
101                         dump('Calling sdump but ' + E + '\n');
102                 }
103         },
104
105         'arg_dump' : function (args,dump_these) {
106                 var s = '*>*>*> Called function ';
107                 try {
108                         if (!dump_these)
109                                 dump_these = {};
110                         s += args.callee.toString().match(/\w+/g)[1] + ' : ';
111                         for (var i = 0; i < args.length; i++)
112                                 s += typeof(args[i]) + ' ';
113                         s += '\n';
114                         for (var i = 0; i < args.length; i++)
115                                 if (dump_these[i]) {
116
117                                         var arg = args[i];
118                                         //dump('dump_these[i] = ' + dump_these[i] + '  arg = ' + arg + '\n');
119
120                                         if (typeof(dump_these[i])=='string') {
121
122                                                 if (dump_these[i].slice(0,1) == '.') {
123                                                         var cmd = 'arg' + dump_these[i];
124                                                         var result;
125                                                         try {
126                                                                 result = eval( cmd );
127                                                         } catch(E) {
128                                                                 result = cmd + ' ==> ' + E;
129                                                         }
130                                                         s += '\targ #' + i + ': ' + cmd + ' = ' + result;
131                                                 } else {
132                                                         var result;
133                                                         try {
134                                                                 result = eval( dump_these[i] );
135                                                         } catch(E) {
136                                                                 result = dump_these[i] + ' ==> ' + E;
137                                                         }
138                                                         s += '\targ #' + i + ': ' + result;
139                                                 }
140         
141                                         } else {
142                                                 s += '\targ #' + i + ' = ';
143                                                 try {
144                                                         //s += js2JSON( arg );
145                                                         s += arg;
146                                                 } catch(E) {
147                                                         s += arg;
148                                                 }
149                                         }
150         
151                                         s += '\n';
152                                         if (this.arg_dump_full)
153                                                 s += 'Definition: ' + args.callee.toString() + '\n';
154         
155                                 }
156                         return s;
157                 } catch(E) {
158                         return s + '\nDEBUG ME: ' + js2JSON(E) + '\n';
159                 }
160         },
161
162         'handle_error' : function (E,annoy) {
163                 var s = '';
164                 if (instanceOf(E,ex)) {
165                         s += E.err_msg();
166                         //s += '\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n';
167                         //s += 'This error was anticipated.\n\n';
168                         //s += js2JSON(E).substr(0,200) + '...\n\n';
169                         if (snd_bad) snd_bad();
170                 } else {
171                         s += '\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n';
172                         s += 'This is a bug that we will fix later.\n\n';
173                         try {
174                                 s += js2JSON(E).substr(0,1024) + '\n\n';
175                         } catch(E2) {
176                                 try {
177                                         s += E.substr(0,1024) + '\n\n';
178                                 } catch(E3) {
179                                         s += E + '\n\n';
180                                 }
181                         }
182                         if (snd_really_bad) snd_really_bad();
183                 }
184                 sdump('D_ERROR',s);
185                 if (annoy)
186                         this.s_alert(s);
187                 else
188                         alert(s);
189         },
190
191         's_alert' : function (s) { alert(s); }
192 }       
193
194 dump('exiting util/error.js\n');