mirror of https://github.com/apache/druid.git
Add Conditional Helpers to DruidException / InvalidInput (#16470)
Adds versions of DruidException.defensive(String, Object...) InvalidInput.exception(String, Object...) InvalidInput.exception(Throwable, String, Object...) the versions add a boolean as the first arg and only create and throw an exception if it's false. It can be used similar to Preconditions.checkState/checkArgument
This commit is contained in:
parent
4c8932e00e
commit
a10310388f
|
@ -47,7 +47,7 @@ import java.util.Map;
|
||||||
* no change should occur.
|
* no change should occur.
|
||||||
* <p>
|
* <p>
|
||||||
* <h3>Notes about exception messages:</h3>
|
* <h3>Notes about exception messages:</h3>
|
||||||
*
|
* <p>
|
||||||
* Firstly, exception messages should always be written with the notions from the style conventions covered in
|
* Firstly, exception messages should always be written with the notions from the style conventions covered in
|
||||||
* {@code dev/style-conventions.md}. Whenever possible, we should also try to provide an action to take to resolve
|
* {@code dev/style-conventions.md}. Whenever possible, we should also try to provide an action to take to resolve
|
||||||
* the issue.
|
* the issue.
|
||||||
|
@ -176,6 +176,22 @@ public class DruidException extends RuntimeException
|
||||||
return defensive().build(format, args);
|
return defensive().build(format, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a "defensive" exception, this is an exception that should never actually be triggered. Throw to
|
||||||
|
* allow messages to be seen by developers
|
||||||
|
*
|
||||||
|
* @param condition - boolean condition to validate
|
||||||
|
* @param msg - passed through to InvalidInput.exception()
|
||||||
|
* @param args - passed through to InvalidInput.exception()
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public static void conditionalDefensive(boolean condition, String msg, Object... args)
|
||||||
|
{
|
||||||
|
if (!condition) {
|
||||||
|
throw defensive(msg, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private final Persona targetPersona;
|
private final Persona targetPersona;
|
||||||
private final Category category;
|
private final Category category;
|
||||||
private final String errorCode;
|
private final String errorCode;
|
||||||
|
|
|
@ -35,6 +35,37 @@ public class InvalidInput extends BaseFailure
|
||||||
return DruidException.fromFailure(new InvalidInput(t, msg, args));
|
return DruidException.fromFailure(new InvalidInput(t, msg, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* evalues a condition. If it's false, it throws the appropriate DruidException
|
||||||
|
*
|
||||||
|
* @param condition - boolean condition to validate
|
||||||
|
* @param msg - passed through to DruidException.exception()
|
||||||
|
* @param args - passed through to DruidException.exception()
|
||||||
|
*/
|
||||||
|
public static void conditionalException(boolean condition, String msg, Object... args)
|
||||||
|
{
|
||||||
|
if (!condition) {
|
||||||
|
throw exception(msg, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* evalues a condition. If it's false, it throws the appropriate DruidException with a given cause
|
||||||
|
*
|
||||||
|
* @param condition - boolean condition to validate
|
||||||
|
* @param t - throwable to pass to InvalidInput.exception()
|
||||||
|
* @param msg - passed through to InvalidInput.exception()
|
||||||
|
* @param args - passed through to InvalidInput.exception()
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static void conditionalException(boolean condition, Throwable t, String msg, Object... args)
|
||||||
|
{
|
||||||
|
if (!condition) {
|
||||||
|
throw exception(t, msg, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public InvalidInput(
|
public InvalidInput(
|
||||||
Throwable t,
|
Throwable t,
|
||||||
String msg,
|
String msg,
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.druid.error;
|
||||||
|
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class InvalidInputTest
|
||||||
|
{
|
||||||
|
@Test
|
||||||
|
public void testConditionalNoThrow()
|
||||||
|
{
|
||||||
|
InvalidInput.conditionalException(true, "This should not throw");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = DruidException.class)
|
||||||
|
public void testConditionalThrow()
|
||||||
|
{
|
||||||
|
InvalidInput.conditionalException(false, "This should throw");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConditionalNoThrowWithCause()
|
||||||
|
{
|
||||||
|
InvalidInput.conditionalException(true, new RuntimeException(), "This should not throw");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = DruidException.class)
|
||||||
|
public void testConditionalThrowWithCause()
|
||||||
|
{
|
||||||
|
InvalidInput.conditionalException(false, new RuntimeException(), "This should throw");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue