In a Cyclops React "Try with Resources" block, I would like to map an IOException
to a custom exception type. I have tried this also with Javaslang and seems less fexible since it treats all exceptions the same.
Code example:
private static Try<String, ConnectionError> readString() {
// Socket is a thread-local static field
final Try<String, IOException> string = Try.catchExceptions(IOException.class)
.init(() -> new BufferedReader(new InputStreamReader(socket.get().getInputStream())))
.tryWithResources(BufferedReader::readLine);
return string.isSuccess() ? Try.success(string.get()) :
Try.failure(new ConnectionError("Could not read from server", string.failureGet()));
}
Could this be done in more elegant way? Or does not make any sense and it would be better to return Try<String, IOException>
?
Disclaimer: I am a newbie using the Cyclops React library and functional programming in general, so I may have severe conceptual miscomprehensions.