Skip to content

Commit

Permalink
add toRuntimeException (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgardens authored Oct 8, 2020
1 parent 22d6833 commit 9602f11
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.concurrent.Callable;

@FunctionalInterface
public interface VoidCallable extends Callable<Void> {

default @Override Void call() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* MIT License
*
* Copyright (c) 2020 Airbyte
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.airbyte.commons.lang;

import java.util.concurrent.Callable;

public class Exceptions {

/**
* Catch a checked exception and rethrow as a {@link RuntimeException}
*
* @param callable - function that throws a checked exception.
* @param <T> - return type of the function.
* @return object that the function returns.
*/
public static <T> T toRuntime(Callable<T> callable) {
try {
return callable.call();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* Catch a checked exception and rethrow as a {@link RuntimeException}.
*
* @param voidCallable - function that throws a checked exception.
*/
public static void toRuntime(Procedure voidCallable) {
try {
voidCallable.call();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public interface Procedure {

void call() throws Exception;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* MIT License
*
* Copyright (c) 2020 Airbyte
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.airbyte.commons.lang;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;

class ExceptionsTest {

@Test
void testToRuntime() {
assertEquals("hello", Exceptions.toRuntime(() -> callable("hello", false)));
assertThrows(RuntimeException.class, () -> Exceptions.toRuntime(() -> callable("goodbye", true)));
}

@Test
void testToRuntimeVoid() {
List<String> list = new ArrayList<>();
assertThrows(RuntimeException.class, () -> Exceptions.toRuntime(() -> voidCallable(list, "hello", true)));
assertEquals(0, list.size());

Exceptions.toRuntime(() -> voidCallable(list, "goodbye", false));
assertEquals(1, list.size());
assertEquals("goodbye", list.get(0));
}

private String callable(String input, boolean shouldThrow) throws IOException {
if (shouldThrow) {
throw new IOException();
} else {
return input;
}
}

private void voidCallable(List<String> list, String input, boolean shouldThrow) throws IOException {
if (shouldThrow) {
throw new IOException();
} else {
list.add(input);
}
}

}

0 comments on commit 9602f11

Please sign in to comment.