Skip to content

Commit 9c64fd2

Browse files
Alioth4Jvlsi
authored andcommitted
factor out error handling logic
1 parent d154f27 commit 9c64fd2

File tree

1 file changed

+18
-43
lines changed

1 file changed

+18
-43
lines changed

Diff for: src/core/src/main/java/org/apache/jmeter/util/BeanShellInterpreter.java

+18-43
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public class BeanShellInterpreter {
4747

4848
private final Logger logger; // Logger to use during initialization and script run
4949

50-
private static final String BSH_ERROR_TEMPLATE = "Error invoking bsh method: %s";
51-
5250
public BeanShellInterpreter() {
5351
this(null, null);
5452
}
@@ -110,13 +108,7 @@ public Object eval(String s) throws JMeterException {
110108
try {
111109
r = bshInstance.eval(s);
112110
} 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);
120112
}
121113
return r;
122114
}
@@ -126,12 +118,7 @@ public Object evalNoLog(String s) throws JMeterException {
126118
try {
127119
r = bshInstance.eval(s);
128120
} 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);
135122
}
136123
return r;
137124
}
@@ -140,27 +127,15 @@ public void set(String s, Object o) throws JMeterException {
140127
try {
141128
bshInstance.set(s, o);
142129
} 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);
150131
}
151132
}
152133

153134
public void set(String s, boolean b) throws JMeterException {
154135
try {
155136
bshInstance.set(s, b);
156137
} 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);
164139
}
165140
}
166141

@@ -169,13 +144,7 @@ public Object source(String s) throws JMeterException {
169144
try {
170145
r = bshInstance.source(s);
171146
} 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);
179148
}
180149
return r;
181150
}
@@ -185,17 +154,23 @@ public Object get(String s) throws JMeterException {
185154
try {
186155
r = bshInstance.get(s);
187156
} 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);
195158
}
196159
return r;
197160
}
198161

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+
199174
// For use by Unit Tests
200175
public static boolean isInterpreterPresent() {
201176
Class<?> bshClass = null;

0 commit comments

Comments
 (0)