Улучшен вывод ошибок

This commit is contained in:
Victor 2019-01-04 23:40:22 +02:00
parent 748c7ee45e
commit 4eabeca2ac
3 changed files with 24 additions and 9 deletions

View File

@ -34,11 +34,13 @@ public final class UserDefinedFunction implements Function {
final int size = values.length; final int size = values.length;
final int requiredArgsCount = arguments.getRequiredArgumentsCount(); final int requiredArgsCount = arguments.getRequiredArgumentsCount();
if (size < requiredArgsCount) { if (size < requiredArgsCount) {
throw new ArgumentsMismatchException(String.format("Arguments count mismatch. %d < %d", size, requiredArgsCount)); throw new ArgumentsMismatchException(String.format(
"Arguments count mismatch. Required %d, got %d", requiredArgsCount, size));
} }
final int totalArgsCount = getArgsCount(); final int totalArgsCount = getArgsCount();
if (size > totalArgsCount) { if (size > totalArgsCount) {
throw new ArgumentsMismatchException(String.format("Arguments count mismatch. %d > %d", size, totalArgsCount)); throw new ArgumentsMismatchException(String.format(
"Arguments count mismatch. Total %d, got %d", totalArgsCount, size));
} }
try { try {

View File

@ -1,5 +1,7 @@
package com.annimon.ownlang.parser.ast; package com.annimon.ownlang.parser.ast;
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.exceptions.VariableDoesNotExistsException; import com.annimon.ownlang.exceptions.VariableDoesNotExistsException;
import com.annimon.ownlang.exceptions.UnknownFunctionException; import com.annimon.ownlang.exceptions.UnknownFunctionException;
import com.annimon.ownlang.lib.*; import com.annimon.ownlang.lib.*;
@ -40,9 +42,13 @@ public final class FunctionalExpression extends InterruptableNode implements Exp
} }
final Function f = consumeFunction(functionExpr); final Function f = consumeFunction(functionExpr);
CallStack.enter(functionExpr.toString(), f); CallStack.enter(functionExpr.toString(), f);
final Value result = f.execute(values); try {
return f.execute(values);
} catch (ArgumentsMismatchException | TypeException | VariableDoesNotExistsException ex) {
throw new RuntimeException(ex.getMessage() + " in function " + functionExpr, ex);
} finally {
CallStack.exit(); CallStack.exit();
return result; }
} }
private Function consumeFunction(Expression expr) { private Function consumeFunction(Expression expr) {
@ -58,7 +64,9 @@ public final class FunctionalExpression extends InterruptableNode implements Exp
} }
private Function getFunction(String key) { private Function getFunction(String key) {
if (Functions.isExists(key)) return Functions.get(key); if (Functions.isExists(key)) {
return Functions.get(key);
}
if (Variables.isExists(key)) { if (Variables.isExists(key)) {
final Value variable = Variables.get(key); final Value variable = Variables.get(key);
if (variable.type() == Types.FUNCTION) { if (variable.type() == Types.FUNCTION) {

View File

@ -36,7 +36,7 @@ public final class UseStatement extends InterruptableNode implements Statement {
loadModule(value.asString()); loadModule(value.asString());
break; break;
default: default:
throw new TypeException("Array or string required"); throw typeException(value);
} }
} }
@ -45,7 +45,7 @@ public final class UseStatement extends InterruptableNode implements Statement {
final Module module = (Module) Class.forName(String.format(PACKAGE, name, name)).newInstance(); final Module module = (Module) Class.forName(String.format(PACKAGE, name, name)).newInstance();
module.init(); module.init();
} catch (Exception ex) { } catch (Exception ex) {
throw new RuntimeException(ex); throw new RuntimeException("Unable to load module " + name, ex);
} }
} }
@ -61,10 +61,15 @@ public final class UseStatement extends InterruptableNode implements Statement {
loadConstants(value.asString()); loadConstants(value.asString());
break; break;
default: default:
throw new TypeException("Array or string required"); throw typeException(value);
} }
} }
private TypeException typeException(Value value) {
return new TypeException("Array or string required in 'use' statement, " +
"got " + Types.typeToString(value.type()) + " " + value);
}
private void loadConstants(String moduleName) { private void loadConstants(String moduleName) {
try { try {
final Class<?> moduleClass = Class.forName(String.format(PACKAGE, moduleName, moduleName)); final Class<?> moduleClass = Class.forName(String.format(PACKAGE, moduleName, moduleName));