@@ -47,8 +47,6 @@ public class BeanShellInterpreter {
47
47
48
48
private final Logger logger ; // Logger to use during initialization and script run
49
49
50
- private static final String BSH_ERROR_TEMPLATE = "Error invoking bsh method: %s" ;
51
-
52
50
public BeanShellInterpreter () {
53
51
this (null , null );
54
52
}
@@ -110,13 +108,7 @@ public Object eval(String s) throws JMeterException {
110
108
try {
111
109
r = bshInstance .eval (s );
112
110
} catch (EvalError e ) {
113
- String message = String .format (BSH_ERROR_TEMPLATE , "eval" );
114
- Throwable cause = e .getCause ();
115
- if (cause != null ) {
116
- message += "\t " + cause .getLocalizedMessage ();
117
- }
118
- log .error (message );
119
- throw new JMeterException (message , e );
111
+ handleException ("eval" , e , true );
120
112
}
121
113
return r ;
122
114
}
@@ -126,12 +118,7 @@ public Object evalNoLog(String s) throws JMeterException {
126
118
try {
127
119
r = bshInstance .eval (s );
128
120
} catch (EvalError e ) {
129
- String message = String .format (BSH_ERROR_TEMPLATE , "eval" );
130
- Throwable cause = e .getCause ();
131
- if (cause != null ) {
132
- message += "\t " + cause .getLocalizedMessage ();
133
- }
134
- throw new JMeterException (message , e );
121
+ handleException ("eval" , e , false );
135
122
}
136
123
return r ;
137
124
}
@@ -140,27 +127,15 @@ public void set(String s, Object o) throws JMeterException {
140
127
try {
141
128
bshInstance .set (s , o );
142
129
} catch (EvalError e ) {
143
- String message = String .format (BSH_ERROR_TEMPLATE , "set" );
144
- Throwable cause = e .getCause ();
145
- if (cause != null ) {
146
- message += "\t " + cause .getLocalizedMessage ();
147
- }
148
- log .error (message );
149
- throw new JMeterException (message , e );
130
+ handleException ("set" , e , true );
150
131
}
151
132
}
152
133
153
134
public void set (String s , boolean b ) throws JMeterException {
154
135
try {
155
136
bshInstance .set (s , b );
156
137
} catch (EvalError e ) {
157
- String message = String .format (BSH_ERROR_TEMPLATE , "set" );
158
- Throwable cause = e .getCause ();
159
- if (cause != null ) {
160
- message += "\t " + cause .getLocalizedMessage ();
161
- }
162
- log .error (message );
163
- throw new JMeterException (message , e );
138
+ handleException ("set" , e , true );
164
139
}
165
140
}
166
141
@@ -169,13 +144,7 @@ public Object source(String s) throws JMeterException {
169
144
try {
170
145
r = bshInstance .source (s );
171
146
} catch (EvalError | IOException e ) {
172
- String message = String .format (BSH_ERROR_TEMPLATE , "source" );
173
- Throwable cause = e .getCause ();
174
- if (cause != null ) {
175
- message += "\t " + cause .getLocalizedMessage ();
176
- }
177
- log .error (message );
178
- throw new JMeterException (message , e );
147
+ handleException ("source" , e , true );
179
148
}
180
149
return r ;
181
150
}
@@ -185,17 +154,23 @@ public Object get(String s) throws JMeterException {
185
154
try {
186
155
r = bshInstance .get (s );
187
156
} catch (EvalError e ) {
188
- String message = String .format (BSH_ERROR_TEMPLATE , "get" );
189
- Throwable cause = e .getCause ();
190
- if (cause != null ) {
191
- message += "\t " + cause .getLocalizedMessage ();
192
- }
193
- log .error (message );
194
- throw new JMeterException (message , e );
157
+ handleException ("get" , e , true );
195
158
}
196
159
return r ;
197
160
}
198
161
162
+ private static void handleException (String methodName , Exception e , boolean shouldLog ) throws JMeterException {
163
+ String message = String .format ("Error invoking bsh method: %s" , methodName );
164
+ Throwable cause = e .getCause ();
165
+ if (cause != null ) {
166
+ message += "\t " + cause .getLocalizedMessage ();
167
+ }
168
+ if (shouldLog ) {
169
+ log .error (message );
170
+ }
171
+ throw new JMeterException (message , e );
172
+ }
173
+
199
174
// For use by Unit Tests
200
175
public static boolean isInterpreterPresent () {
201
176
Class <?> bshClass = null ;
0 commit comments