001 /*****************************************************************************
002 * *
003 * This file is part of the BeanShell Java Scripting distribution. *
004 * Documentation and updates may be found at http://www.beanshell.org/ *
005 * *
006 * Sun Public License Notice: *
007 * *
008 * The contents of this file are subject to the Sun Public License Version *
009 * 1.0 (the "License"); you may not use this file except in compliance with *
010 * the License. A copy of the License is available at http://www.sun.com *
011 * *
012 * The Original Code is BeanShell. The Initial Developer of the Original *
013 * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
014 * (C) 2000. All Rights Reserved. *
015 * *
016 * GNU Public License Notice: *
017 * *
018 * Alternatively, the contents of this file may be used under the terms of *
019 * the GNU Lesser General Public License (the "LGPL"), in which case the *
020 * provisions of LGPL are applicable instead of those above. If you wish to *
021 * allow use of your version of this file only under the terms of the LGPL *
022 * and not to allow others to use your version of this file under the SPL, *
023 * indicate your decision by deleting the provisions above and replace *
024 * them with the notice and other provisions required by the LGPL. If you *
025 * do not delete the provisions above, a recipient may use your version of *
026 * this file under either the SPL or the LGPL. *
027 * *
028 * Patrick Niemeyer (pat@pat.net) *
029 * Author of Learning Java, O'Reilly & Associates *
030 * http://www.pat.net/~pat/ *
031 * *
032 *****************************************************************************/
033
034
035 package bsh;
036
037 /**
038 EvalError indicates that we cannot continue evaluating the script
039 or the script has thrown an exception.
040
041 EvalError may be thrown for a script syntax error, an evaluation
042 error such as referring to an undefined variable, an internal error.
043 <p>
044
045 @see TargetError
046 */
047 public class EvalError extends Exception
048 {
049 SimpleNode node;
050
051 // Note: no way to mutate the Throwable message, must maintain our own
052 String message;
053
054 CallStack callstack;
055
056 public EvalError( String s, SimpleNode node, CallStack callstack ) {
057 setMessage(s);
058 this.node = node;
059 // freeze the callstack for the stack trace.
060 if ( callstack != null )
061 this.callstack = callstack.copy();
062 }
063
064 /**
065 Print the error with line number and stack trace.
066 */
067 public String toString()
068 {
069 String trace;
070 if ( node != null )
071 trace = " : at Line: "+ node.getLineNumber()
072 + " : in file: "+ node.getSourceFile()
073 + " : "+node.getText();
074 else
075 // Users should not normally see this.
076 trace = ": <at unknown location>";
077
078 if ( callstack != null )
079 trace = trace +"\n" + getScriptStackTrace();
080
081 return getMessage() + trace;
082 }
083
084 /**
085 Re-throw the error, prepending the specified message.
086 */
087 public void reThrow( String msg )
088 throws EvalError
089 {
090 prependMessage( msg );
091 throw this;
092 }
093
094 /**
095 The error has trace info associated with it.
096 i.e. It has an AST node that can print its location and source text.
097 */
098 SimpleNode getNode() {
099 return node;
100 }
101
102 void setNode( SimpleNode node ) {
103 this.node = node;
104 }
105
106 public String getErrorText() {
107 if ( node != null )
108 return node.getText() ;
109 else
110 return "<unknown error>";
111 }
112
113 public int getErrorLineNumber() {
114 if ( node != null )
115 return node.getLineNumber() ;
116 else
117 return -1;
118 }
119
120 public String getErrorSourceFile() {
121 if ( node != null )
122 return node.getSourceFile() ;
123 else
124 return "<unknown file>";
125 }
126
127 public String getScriptStackTrace()
128 {
129 if ( callstack == null )
130 return "<Unknown>";
131
132 String trace = "";
133 CallStack stack = callstack.copy();
134 while ( stack.depth() > 0 )
135 {
136 NameSpace ns = stack.pop();
137 SimpleNode node = ns.getNode();
138 if ( ns.isMethod )
139 {
140 trace = trace + "\nCalled from method: " + ns.getName();
141 if ( node != null )
142 trace += " : at Line: "+ node.getLineNumber()
143 + " : in file: "+ node.getSourceFile()
144 + " : "+node.getText();
145 }
146 }
147
148 return trace;
149 }
150
151 /**
152 @see #toString() for a full display of the information
153 */
154 public String getMessage() { return message; }
155
156 public void setMessage( String s ) { message = s; }
157
158 /**
159 Prepend the message if it is non-null.
160 */
161 protected void prependMessage( String s )
162 {
163 if ( s == null )
164 return;
165
166 if ( message == null )
167 message = s;
168 else
169 message = s + " : "+ message;
170 }
171
172 }
173