-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathTargetError.java
More file actions
114 lines (101 loc) · 4.93 KB
/
TargetError.java
File metadata and controls
114 lines (101 loc) · 4.93 KB
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
/*****************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
* *
* *
* This file is part of the BeanShell Java Scripting distribution. *
* Documentation and updates may be found at http://www.beanshell.org/ *
* Patrick Niemeyer ([email protected]) *
* Author of Learning Java, O'Reilly & Associates *
* *
*****************************************************************************/
package bsh;
import java.lang.reflect.InvocationTargetException;
import java.io.PrintStream;
/**
TargetError is an EvalError that wraps an exception thrown by the script
(or by code called from the script). TargetErrors indicate exceptions
which can be caught within the script itself, whereas a general EvalError
indicates that the script cannot be evaluated further for some reason.
If the exception is caught within the script it is automatically unwrapped,
so the code looks like normal Java code. If the TargetError is thrown
from the eval() or interpreter.eval() method it may be caught and unwrapped
to determine what exception was thrown.
*/
public final class TargetError extends EvalError
{
private final boolean inNativeCode;
public TargetError(
String msg, Throwable t, Node node, CallStack callstack,
boolean inNativeCode )
{
super( msg, node, callstack, t );
this.inNativeCode = inNativeCode;
}
public TargetError( Throwable t, Node node, CallStack callstack )
{
this("TargetError", t, node, callstack, false);
}
public synchronized Throwable getTarget()
{
// check for easy mistake
final Throwable target = getCause();
if(target instanceof InvocationTargetException)
return target.getCause();
else
return target;
}
public synchronized String getMessage()
{
return super.getMessage()
+ "Caused by: " +
printTargetError( getCause() );
}
public void printStackTrace( boolean debug, PrintStream out ) {
if ( debug ) {
super.printStackTrace( out );
out.println("--- Target Stack Trace ---");
}
StackTraceElement[] st = getCause().getStackTrace();
for ( StackTraceElement ste : st )
if ( !ste.getClassName().contains("reflect") )
out.println(" at "+ste);
else break;
}
/** Generate a printable string showing the wrapped target exceptions.
* @param t wrapped target exception
* @return messages unwrapped */
private synchronized String printTargetError( Throwable t ) {
if (null == t) return "Cause is null";
StringBuilder msgs = new StringBuilder(t.toString());
while ( null != (t = t.getCause()) )
msgs.append("\n").append(t.toString());
return msgs.toString();
}
/**
Return true if the TargetError was generated from native code.
e.g. if the script called into a compiled java class which threw
the excpetion. We distinguish so that we can print the stack trace
for the native code case... the stack trace would not be useful if
the exception was generated by the script. e.g. if the script
explicitly threw an exception... (the stack trace would simply point
to the bsh internals which generated the exception).
*/
public boolean inNativeCode() {
return inNativeCode;
}
}