Add header.
[amitay/samba.git] / source4 / lib / appweb / ejs-2.0 / ejs / classes / ejsError.c
1 /*
2  *      @file   ejsError.c
3  *      @brief  Error class
4  */
5 /********************************* Copyright **********************************/
6 /*
7  *      @copy   default
8  *      
9  *      Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
10  *      Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
11  *      
12  *      This software is distributed under commercial and open source licenses.
13  *      You may use the GPL open source license described below or you may acquire 
14  *      a commercial license from Mbedthis Software. You agree to be fully bound 
15  *      by the terms of either license. Consult the LICENSE.TXT distributed with 
16  *      this software for full details.
17  *      
18  *      This software is open source; you can redistribute it and/or modify it 
19  *      under the terms of the GNU General Public License as published by the 
20  *      Free Software Foundation; either version 2 of the License, or (at your 
21  *      option) any later version. See the GNU General Public License for more 
22  *      details at: http://www.mbedthis.com/downloads/gplLicense.html
23  *      
24  *      This program is distributed WITHOUT ANY WARRANTY; without even the 
25  *      implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
26  *      
27  *      This GPL license does NOT permit incorporating this software into 
28  *      proprietary programs. If you are unable to comply with the GPL, you must
29  *      acquire a commercial license to use this software. Commercial licenses 
30  *      for this software and support services are available from Mbedthis 
31  *      Software at http://www.mbedthis.com 
32  *      
33  *      @end
34  */
35 /********************************** Includes **********************************/
36
37 #include        "ejs.h"
38
39 #if BLD_FEATURE_EJS
40
41 /************************************ Code ************************************/
42 /*
43  *      Parse the args and return the message. Convert non-string args using
44  *      .toString.
45  */
46
47 static char *getMessage(Ejs *ep, int argc, EjsVar **argv)
48 {
49         if (argc == 0) {
50                 return "";
51
52         } else if (argc == 1) {
53                 if (! ejsVarIsString(argv[0])) {
54                         if (ejsRunMethod(ep, argv[0], "toString", 0) < 0) {
55                                 return 0;
56                         }
57                         return ep->result->string;
58
59                 } else {
60                         return argv[0]->string;
61                 }
62
63         } else {
64                 /* Don't call ejsError here or it will go recursive. */
65                 return 0;
66         }
67 }
68
69
70 /******************************************************************************/
71 /*
72  *      Error Constructor and also used for constructor for sub classes. 
73  *
74  *      Usage: new Error([message])
75  */
76
77 int ejsErrorCons(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
78 {
79         char    *msg, *stack;
80
81         msg = getMessage(ep, argc, argv);
82         if (msg == 0) {
83                 return -1;
84         }
85
86         ejsSetPropertyToString(ep, thisObj, "name", ejsGetBaseClassName(thisObj));
87         ejsSetPropertyToString(ep, thisObj, "message", msg);
88
89         ejsSetPropertyToUndefined(ep, thisObj, "stack");
90
91         stack = ejsFormatStack(ep);
92         if (stack) {
93                 ejsSetPropertyToString(ep, thisObj, "stack", stack);
94                 mprFree(stack);
95         }
96
97         if (ejsObjHasErrors(thisObj)) {
98                 return -1;
99         }
100
101         return 0;
102 }
103
104 /******************************************************************************/
105
106 int ejsDefineErrorClasses(Ejs *ep)
107 {
108         if (ejsDefineClass(ep, "Error", "Object", ejsErrorCons) == 0 ||
109                 ejsDefineClass(ep, "AssertError", "Error", ejsErrorCons) == 0 ||
110                 ejsDefineClass(ep, "EvalError", "Error", ejsErrorCons) == 0 ||
111                 ejsDefineClass(ep, "InternalError", "Error", ejsErrorCons) == 0 ||
112                 ejsDefineClass(ep, "IOError", "Error", ejsErrorCons) == 0 ||
113                 ejsDefineClass(ep, "MemoryError", "Error", ejsErrorCons) == 0 ||
114                 ejsDefineClass(ep, "RangeError", "Error", ejsErrorCons) == 0 ||
115                 ejsDefineClass(ep, "ReferenceError", "Error", ejsErrorCons) == 0 ||
116                 ejsDefineClass(ep, "SyntaxError", "Error", ejsErrorCons) == 0 ||
117                 ejsDefineClass(ep, "TypeError", "Error", ejsErrorCons) == 0) {
118
119                 return MPR_ERR_CANT_INITIALIZE;
120         }
121         return 0;
122 }
123
124 /******************************************************************************/
125
126 #else
127 void ejsErrorDummy() {}
128
129 /******************************************************************************/
130 #endif /* BLD_FEATURE_EJS */
131
132 /*
133  * Local variables:
134  * tab-width: 4
135  * c-basic-offset: 4
136  * End:
137  * vim:tw=78
138  * vim600: sw=4 ts=4 fdm=marker
139  * vim<600: sw=4 ts=4
140  */