Skip to content

Commit dd375f8

Browse files
committed
Drop annoying IOException from task methods
It's only Service#start that really can throw IOException. Rather than letting the lazy service starts propagate the checked IOException to many methods downstream, we can wrap service autostart failures in UncheckedIOException. Slightly smelly? Yes. Super-convenient? Also yes.
1 parent 67fad4b commit dd375f8

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/main/java/org/apposed/appose/Service.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.io.IOException;
4040
import java.io.InputStreamReader;
4141
import java.io.PrintWriter;
42+
import java.io.UncheckedIOException;
4243
import java.nio.charset.StandardCharsets;
4344
import java.nio.file.Files;
4445
import java.util.ArrayList;
@@ -176,9 +177,9 @@ public Service start() throws IOException {
176177
*
177178
* @param script The script for the worker to execute in its environment.
178179
* @return The newly created {@link Task} object tracking the execution.
179-
* @throws IOException If something goes wrong communicating with the worker.
180+
* @throws UncheckedIOException If something goes wrong auto-starting the service.
180181
*/
181-
public Task task(String script) throws IOException {
182+
public Task task(String script) {
182183
return task(script, null, null);
183184
}
184185

@@ -188,9 +189,9 @@ public Task task(String script) throws IOException {
188189
* @param script The script for the worker to execute in its environment.
189190
* @param inputs Optional list of key/value pairs to feed into the script as inputs.
190191
* @return The newly created {@link Task} object tracking the execution.
191-
* @throws IOException If something goes wrong communicating with the worker.
192+
* @throws UncheckedIOException If something goes wrong auto-starting the service.
192193
*/
193-
public Task task(String script, Map<String, Object> inputs) throws IOException {
194+
public Task task(String script, Map<String, Object> inputs) {
194195
return task(script, inputs, null);
195196
}
196197

@@ -200,9 +201,9 @@ public Task task(String script, Map<String, Object> inputs) throws IOException {
200201
* @param script The script for the worker to execute in its environment.
201202
* @param queue Optional queue target. Pass "main" to queue to worker's main thread.
202203
* @return The newly created {@link Task} object tracking the execution.
203-
* @throws IOException If something goes wrong communicating with the worker.
204+
* @throws UncheckedIOException If something goes wrong auto-starting the service.
204205
*/
205-
public Task task(String script, String queue) throws IOException {
206+
public Task task(String script, String queue) {
206207
return task(script, null, queue);
207208
}
208209

@@ -213,10 +214,15 @@ public Task task(String script, String queue) throws IOException {
213214
* @param inputs Optional list of key/value pairs to feed into the script as inputs.
214215
* @param queue Optional queue target. Pass "main" to queue to worker's main thread.
215216
* @return The newly created {@link Task} object tracking the execution.
216-
* @throws IOException If something goes wrong communicating with the worker.
217+
* @throws UncheckedIOException If something goes wrong auto-starting the service.
217218
*/
218-
public Task task(String script, Map<String, Object> inputs, String queue) throws IOException {
219-
start();
219+
public Task task(String script, Map<String, Object> inputs, String queue) {
220+
try {
221+
start();
222+
}
223+
catch (IOException exc) {
224+
throw new UncheckedIOException("Service autostart failed", exc);
225+
}
220226
return new Task(script, inputs, queue);
221227
}
222228

@@ -277,12 +283,11 @@ public ScriptSyntax syntax() {
277283
*
278284
* @param name The name of the variable to retrieve from the worker process.
279285
* @return The value of the variable.
280-
* @throws IOException If something goes wrong communicating with the worker.
281286
* @throws InterruptedException If the current thread is interrupted while waiting.
282287
* @throws TaskException If the task fails to retrieve the variable.
283288
* @throws IllegalStateException If no script syntax has been configured for this service.
284289
*/
285-
public Object getVar(String name) throws IOException, InterruptedException, TaskException {
290+
public Object getVar(String name) throws InterruptedException, TaskException {
286291
Syntaxes.validate(this);
287292
String script = syntax.getVar(name);
288293
Task task = task(script).waitFor();
@@ -301,12 +306,11 @@ public Object getVar(String name) throws IOException, InterruptedException, Task
301306
*
302307
* @param name The name of the variable to set in the worker process.
303308
* @param value The value to assign to the variable.
304-
* @throws IOException If something goes wrong communicating with the worker.
305309
* @throws InterruptedException If the current thread is interrupted while waiting.
306310
* @throws TaskException If the task fails to set the variable.
307311
* @throws IllegalStateException If no script syntax has been configured for this service.
308312
*/
309-
public void putVar(String name, Object value) throws IOException, InterruptedException, TaskException {
313+
public void putVar(String name, Object value) throws InterruptedException, TaskException {
310314
Syntaxes.validate(this);
311315
Map<String, Object> inputs = new HashMap<>();
312316
inputs.put("_value", value);
@@ -330,12 +334,11 @@ public void putVar(String name, Object value) throws IOException, InterruptedExc
330334
* @param function The name of the function to call in the worker process.
331335
* @param args The arguments to pass to the function.
332336
* @return The result of the function call.
333-
* @throws IOException If something goes wrong communicating with the worker.
334337
* @throws InterruptedException If the current thread is interrupted while waiting.
335338
* @throws TaskException If the function call fails.
336339
* @throws IllegalStateException If no script syntax has been configured for this service.
337340
*/
338-
public Object call(String function, Object... args) throws IOException, InterruptedException, TaskException {
341+
public Object call(String function, Object... args) throws InterruptedException, TaskException {
339342
Syntaxes.validate(this);
340343
Map<String, Object> inputs = new HashMap<>();
341344
List<String> varNames = new ArrayList<>();

0 commit comments

Comments
 (0)