2003/05/12 05:10:30
[org.ibex.core.git] / src / org / mozilla / javascript / JavaScriptException.java
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r
2  *\r
3  * The contents of this file are subject to the Netscape Public\r
4  * License Version 1.1 (the "License"); you may not use this file\r
5  * except in compliance with the License. You may obtain a copy of\r
6  * the License at http://www.mozilla.org/NPL/\r
7  *\r
8  * Software distributed under the License is distributed on an "AS\r
9  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr\r
10  * implied. See the License for the specific language governing\r
11  * rights and limitations under the License.\r
12  *\r
13  * The Original Code is Rhino code, released\r
14  * May 6, 1999.\r
15  *\r
16  * The Initial Developer of the Original Code is Netscape\r
17  * Communications Corporation.  Portions created by Netscape are\r
18  * Copyright (C) 1997-1999 Netscape Communications Corporation. All\r
19  * Rights Reserved.\r
20  *\r
21  * Contributor(s): \r
22  * Norris Boyd\r
23  *\r
24  * Alternatively, the contents of this file may be used under the\r
25  * terms of the GNU Public License (the "GPL"), in which case the\r
26  * provisions of the GPL are applicable instead of those above.\r
27  * If you wish to allow use of your version of this file only\r
28  * under the terms of the GPL and not to allow others to use your\r
29  * version of this file under the NPL, indicate your decision by\r
30  * deleting the provisions above and replace them with the notice\r
31  * and other provisions required by the GPL.  If you do not delete\r
32  * the provisions above, a recipient may use your version of this\r
33  * file under either the NPL or the GPL.\r
34  */\r
35 \r
36 // API class\r
37 \r
38 package org.mozilla.javascript;\r
39 \r
40 import java.lang.reflect.InvocationTargetException;\r
41 \r
42 /**\r
43  * Java reflection of JavaScript exceptions.  (Possibly wrapping a Java exception.)\r
44  *\r
45  * @author Mike McCabe\r
46  */\r
47 public class JavaScriptException extends Exception {\r
48 \r
49     public int line;\r
50     public String sourceFile;\r
51 \r
52     /**\r
53      * Create a JavaScript exception wrapping the given JavaScript value.\r
54      *\r
55      * Instances of this class are thrown by the JavaScript 'throw' keyword.\r
56      *\r
57      * @param value the JavaScript value thrown.\r
58      */\r
59     public JavaScriptException(Object value) {\r
60         super(ScriptRuntime.toString(value));\r
61         line = Context.enter().interpreterLine;\r
62         sourceFile = Context.enter().interpreterSourceFile;\r
63         this.value = value;\r
64     }\r
65 \r
66     /**\r
67      * Get the exception message.\r
68      *\r
69      * <p>Will just convert the wrapped exception to a string.\r
70      */\r
71     public String getMessage() {\r
72         return ScriptRuntime.toString(value);\r
73     }\r
74 \r
75     static JavaScriptException wrapException(Scriptable scope,\r
76                                              Throwable exn)\r
77     {\r
78         if (exn instanceof InvocationTargetException)\r
79             exn = ((InvocationTargetException)exn).getTargetException();\r
80         if (exn instanceof JavaScriptException)\r
81             return (JavaScriptException)exn;\r
82         Object wrapper = NativeJavaObject.wrap(scope, exn, Throwable.class);\r
83         return new JavaScriptException(wrapper);\r
84     }\r
85 \r
86     /**\r
87      * Get the exception value originally thrown.  This may be a\r
88      * JavaScript value (null, undefined, Boolean, Number, String,\r
89      * Scriptable or Function) or a Java exception value thrown from a\r
90      * host object or from Java called through LiveConnect.\r
91      *\r
92      * @return the value wrapped by this exception\r
93      */\r
94     public Object getValue() {\r
95         if (value != null && value instanceof Wrapper)\r
96             // this will also catch NativeStrings...\r
97             return ((Wrapper)value).unwrap();\r
98         else\r
99             return value;\r
100     }\r
101 \r
102     /**\r
103      * The JavaScript exception value.  This value is not\r
104      * intended for general use; if the JavaScriptException wraps a\r
105      * Java exception, getScriptableValue may return a Scriptable\r
106      * wrapping the original Java exception object.\r
107      *\r
108      * We would prefer to go through a getter to encapsulate the value,\r
109      * however that causes the bizarre error "nanosecond timeout value \r
110      * out of range" on the MS JVM. \r
111      * @serial \r
112      */\r
113     Object value;\r
114 }\r