Skip to content

Commit 4eabeca

Browse files
committed
Улучшен вывод ошибок
1 parent 748c7ee commit 4eabeca

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/main/java/com/annimon/ownlang/lib/UserDefinedFunction.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ public Value execute(Value... values) {
3434
final int size = values.length;
3535
final int requiredArgsCount = arguments.getRequiredArgumentsCount();
3636
if (size < requiredArgsCount) {
37-
throw new ArgumentsMismatchException(String.format("Arguments count mismatch. %d < %d", size, requiredArgsCount));
37+
throw new ArgumentsMismatchException(String.format(
38+
"Arguments count mismatch. Required %d, got %d", requiredArgsCount, size));
3839
}
3940
final int totalArgsCount = getArgsCount();
4041
if (size > totalArgsCount) {
41-
throw new ArgumentsMismatchException(String.format("Arguments count mismatch. %d > %d", size, totalArgsCount));
42+
throw new ArgumentsMismatchException(String.format(
43+
"Arguments count mismatch. Total %d, got %d", totalArgsCount, size));
4244
}
4345

4446
try {

src/main/java/com/annimon/ownlang/parser/ast/FunctionalExpression.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.annimon.ownlang.parser.ast;
22

3+
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
4+
import com.annimon.ownlang.exceptions.TypeException;
35
import com.annimon.ownlang.exceptions.VariableDoesNotExistsException;
46
import com.annimon.ownlang.exceptions.UnknownFunctionException;
57
import com.annimon.ownlang.lib.*;
@@ -40,9 +42,13 @@ public Value eval() {
4042
}
4143
final Function f = consumeFunction(functionExpr);
4244
CallStack.enter(functionExpr.toString(), f);
43-
final Value result = f.execute(values);
44-
CallStack.exit();
45-
return result;
45+
try {
46+
return f.execute(values);
47+
} catch (ArgumentsMismatchException | TypeException | VariableDoesNotExistsException ex) {
48+
throw new RuntimeException(ex.getMessage() + " in function " + functionExpr, ex);
49+
} finally {
50+
CallStack.exit();
51+
}
4652
}
4753

4854
private Function consumeFunction(Expression expr) {
@@ -58,7 +64,9 @@ private Function consumeFunction(Expression expr) {
5864
}
5965

6066
private Function getFunction(String key) {
61-
if (Functions.isExists(key)) return Functions.get(key);
67+
if (Functions.isExists(key)) {
68+
return Functions.get(key);
69+
}
6270
if (Variables.isExists(key)) {
6371
final Value variable = Variables.get(key);
6472
if (variable.type() == Types.FUNCTION) {

src/main/java/com/annimon/ownlang/parser/ast/UseStatement.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void execute() {
3636
loadModule(value.asString());
3737
break;
3838
default:
39-
throw new TypeException("Array or string required");
39+
throw typeException(value);
4040
}
4141
}
4242

@@ -45,7 +45,7 @@ private void loadModule(String name) {
4545
final Module module = (Module) Class.forName(String.format(PACKAGE, name, name)).newInstance();
4646
module.init();
4747
} catch (Exception ex) {
48-
throw new RuntimeException(ex);
48+
throw new RuntimeException("Unable to load module " + name, ex);
4949
}
5050
}
5151

@@ -61,10 +61,15 @@ public void loadConstants() {
6161
loadConstants(value.asString());
6262
break;
6363
default:
64-
throw new TypeException("Array or string required");
64+
throw typeException(value);
6565
}
6666
}
6767

68+
private TypeException typeException(Value value) {
69+
return new TypeException("Array or string required in 'use' statement, " +
70+
"got " + Types.typeToString(value.type()) + " " + value);
71+
}
72+
6873
private void loadConstants(String moduleName) {
6974
try {
7075
final Class<?> moduleClass = Class.forName(String.format(PACKAGE, moduleName, moduleName));

0 commit comments

Comments
 (0)