refactor: Alter handling of errors in bindings (#1395)

* Alter handling of errors in bindings
 - Fixes #1384

* Arbitrarily use TextComponent#of for InputParseException
This commit is contained in:
Jordan 2021-11-11 17:37:25 +00:00 committed by GitHub
parent 806ea14ad2
commit 6df16cfe96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 14 deletions

View File

@ -101,11 +101,15 @@ public class Bindings {
@Override
public ConversionResult<Object> convert(String s, InjectedValueAccess access) {
Object o = invoke(s, argsFunc, access, method);
if (o == null) {
return FailedConversion.from(new NullPointerException());
try {
Object o = invoke(s, argsFunc, access, method);
if (o == null) {
return FailedConversion.from(new NullPointerException());
}
return SuccessfulConversion.fromSingle(o);
} catch (Throwable t) {
return FailedConversion.from(t);
}
return SuccessfulConversion.fromSingle(o);
}
});
}

View File

@ -27,6 +27,7 @@ import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockType;
import org.enginehub.piston.exception.StopExecutionException;
import org.enginehub.piston.inject.InjectedValueAccess;
import java.util.UUID;
@ -49,36 +50,76 @@ public class ConsumeBindings extends Bindings {
@Binding
@Confirm(Confirm.Processor.REGION)
public int regionMultiple(Actor actor, InjectedValueAccess context, @Selection Region region, String argument) {
int times = (int) Expression.compile(argument).evaluate();
return Confirm.Processor.REGION.check(actor, context, times);
try {
int times = (int) Expression.compile(argument).evaluate();
return Confirm.Processor.REGION.check(actor, context, times);
} catch (Throwable t) {
if (t instanceof StopExecutionException) { // Maintain throw from Confirm#check
throw t;
} else {
throw new InputParseException(TextComponent.of(t.getMessage()), t);
}
}
}
@Binding
@Confirm(Confirm.Processor.RADIUS)
public Integer radiusInteger(Actor actor, InjectedValueAccess context, String argument) {
int times = (int) Expression.compile(argument).evaluate();
return Confirm.Processor.RADIUS.check(actor, context, times);
try {
int times = (int) Expression.compile(argument).evaluate();
return Confirm.Processor.RADIUS.check(actor, context, times);
} catch (Throwable t) {
if (t instanceof StopExecutionException) { // Maintain throw from Confirm#check
throw t;
} else {
throw new InputParseException(TextComponent.of(t.getMessage()), t);
}
}
}
@Binding
@Confirm(Confirm.Processor.LIMIT)
public Integer limitInteger(Actor actor, InjectedValueAccess context, String argument) {
int times = (int) Expression.compile(argument).evaluate();
return Confirm.Processor.LIMIT.check(actor, context, times);
try {
int times = (int) Expression.compile(argument).evaluate();
return Confirm.Processor.LIMIT.check(actor, context, times);
} catch (Throwable t) {
if (t instanceof StopExecutionException) { // Maintain throw from Confirm#check
throw t;
} else {
throw new InputParseException(TextComponent.of(t.getMessage()), t);
}
}
}
@Binding
@Confirm(Confirm.Processor.RADIUS)
public Double radiusDouble(Actor actor, InjectedValueAccess context, String argument) {
double times = Expression.compile(argument).evaluate();
return Confirm.Processor.RADIUS.check(actor, context, times);
try {
double times = Expression.compile(argument).evaluate();
return Confirm.Processor.RADIUS.check(actor, context, times);
} catch (Throwable t) {
if (t instanceof StopExecutionException) { // Maintain throw from Confirm#check
throw t;
} else {
throw new InputParseException(TextComponent.of(t.getMessage()), t);
}
}
}
@Binding
@Confirm(Confirm.Processor.LIMIT)
public Double limitDouble(Actor actor, InjectedValueAccess context, String argument) {
double times = Expression.compile(argument).evaluate();
return Confirm.Processor.LIMIT.check(actor, context, times);
try {
double times = Expression.compile(argument).evaluate();
return Confirm.Processor.LIMIT.check(actor, context, times);
} catch (Throwable t) {
if (t instanceof StopExecutionException) { // Maintain throw from Confirm#check
throw t;
} else {
throw new InputParseException(TextComponent.of(t.getMessage()), t);
}
}
}
@Binding