Skip to content

Commit 7290a70

Browse files
improve error handling
1 parent e6b8e36 commit 7290a70

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+219
-103
lines changed

Code/ArgumentSystem/ProvidedArguments.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace SER.Code.ArgumentSystem;
2020

2121
public class ProvidedArguments(Method method)
2222
{
23-
private Dictionary<(string name, Type type), List<DynamicTryGet>> Arguments { get; } = [];
23+
private Dictionary<(string name, Type type), List<DynamicTryGet>> ArgumentValues { get; } = [];
2424

2525
public Database GetDatabase(string argName)
2626
{
@@ -211,7 +211,7 @@ private List<DynamicTryGet<TValue>> GetEvaluators<TValue, TArg>(string argName)
211211
{
212212
if (evaluator.Result.HasErrored(out var error))
213213
{
214-
throw new ScriptRuntimeError(mainErr + error);
214+
throw new CustomScriptRuntimeError(mainErr + error);
215215
}
216216

217217
if (evaluator is not DynamicTryGet<TValue> argEvalRes)
@@ -226,7 +226,7 @@ private List<DynamicTryGet<TValue>> GetEvaluators<TValue, TArg>(string argName)
226226

227227
private List<DynamicTryGet> GetValueInternal<TValue, TArg>(string argName)
228228
{
229-
if (Arguments.TryGetValue((argName, typeof(TArg)), out var value))
229+
if (ArgumentValues.TryGetValue((argName, typeof(TArg)), out var value))
230230
{
231231
return value;
232232
}
@@ -239,18 +239,28 @@ private List<DynamicTryGet> GetValueInternal<TValue, TArg>(string argName)
239239

240240
if (foundArg.DefaultValue is null)
241241
{
242-
throw new ScriptRuntimeError($"Method '{method.Name}' is missing required argument '{argName}'.");
242+
throw new CustomScriptRuntimeError($"{method} is missing a required argument '{argName}'.");
243243
}
244244

245245
return foundArg.DefaultValue.Value switch
246246
{
247-
TValue argValue => [new DynamicTryGet<TValue>(argValue)],
248-
IEnumerable<TValue> listValue => listValue.Select(DynamicTryGet (v) => new DynamicTryGet<TValue>(v)).ToList(),
249-
null => [new DynamicTryGet<TValue>((TValue)(object)null!)], // magik
247+
TValue argValue => [
248+
new DynamicTryGet<TValue>(argValue)
249+
],
250+
251+
IEnumerable<TValue> listValue => listValue
252+
.Select(DynamicTryGet (v) => new DynamicTryGet<TValue>(v))
253+
.ToList(),
254+
255+
null => [
256+
new DynamicTryGet<TValue>((TValue)(object)null!)
257+
], // magik
258+
250259
_ => throw new AndrzejFuckedUpException(
251260
$"Argument {argName} for method {method.Name} has its default value set to type " +
252261
$"{foundArg.DefaultValue?.GetType().AccurateName ?? "null"}, expected of type {typeof(TValue).Name} or a list of " +
253-
$"{typeof(TValue).Name}s.")
262+
$"{typeof(TValue).Name}s."
263+
)
254264
};
255265
}
256266

@@ -259,10 +269,10 @@ public void Add(ArgumentValueInfo valueInfo)
259269
Log.Debug($"adding {valueInfo.Name} for method {method.Name} ({method.GetHashCode()})");
260270
if (!valueInfo.IsPartOfCollection)
261271
{
262-
Arguments.Add((valueInfo.Name, valueInfo.ArgumentType), [valueInfo.Evaluator]);
272+
ArgumentValues.Add((valueInfo.Name, valueInfo.ArgumentType), [valueInfo.Evaluator]);
263273
return;
264274
}
265275

266-
Arguments.AddOrInitListWithKey((valueInfo.Name, valueInfo.ArgumentType), valueInfo.Evaluator);
276+
ArgumentValues.AddOrInitListWithKey((valueInfo.Name, valueInfo.ArgumentType), valueInfo.Evaluator);
267277
}
268278
}

Code/ContextSystem/BaseContexts/Context.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using SER.Code.ContextSystem.Structures;
2-
using SER.Code.Helpers.Extensions;
32
using SER.Code.Helpers.ResultSystem;
43
using SER.Code.ScriptSystem;
54
using SER.Code.TokenSystem.Tokens;
@@ -8,14 +7,14 @@ namespace SER.Code.ContextSystem.BaseContexts;
87

98
public abstract class Context
109
{
11-
public string Name => GetType().FriendlyTypeName();
12-
1310
public required Script Script { get; set; } = null!;
14-
11+
1512
public required uint? LineNum { get; set; }
1613

1714
public StatementContext? ParentContext { get; set; } = null;
1815

16+
protected abstract string FriendlyName { get; }
17+
1918
public abstract TryAddTokenRes TryAddToken(BaseToken token);
2019

2120
public abstract Result VerifyCurrentState();
@@ -30,6 +29,9 @@ public static Context Create(Type contextType, (Script scr, uint? lineNum) info)
3029

3130
public override string ToString()
3231
{
33-
return Name;
32+
if (LineNum.HasValue)
33+
return $"{FriendlyName} at line {LineNum}";
34+
35+
return FriendlyName;
3436
}
3537
}

Code/ContextSystem/BaseContexts/LoopSingleIterationVariableContext.cs renamed to Code/ContextSystem/BaseContexts/LoopContextWithSingleIterationVariable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace SER.Code.ContextSystem.BaseContexts;
99

10-
public abstract class LoopSingleIterationVariableContext<TVal> : LoopContext, IAcceptOptionalVariableDefinitions
10+
public abstract class LoopContextWithSingleIterationVariable<TVal> : LoopContext, IAcceptOptionalVariableDefinitions
1111
where TVal : Value
1212
{
1313
private VariableToken? _iterationVariableToken;

Code/ContextSystem/BaseContexts/StatementContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public abstract class StatementContext : YieldingContext
1010

1111
public void SendControlMessage(ParentContextControlMessage msg)
1212
{
13-
Log.Debug($"{Name} context has received control message: {msg}");
13+
Log.Debug($"{this} has received control message: {msg}");
1414
OnReceivedControlMessageFromChild(msg);
1515
}
1616

Code/ContextSystem/BaseContexts/YieldingContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public IEnumerator<float> Run()
1313
}
1414

1515
var prof = Script.Profile is not null
16-
? new Profile(Script.Profile, $"running YieldingContext {Name}")
16+
? new Profile(Script.Profile, $"running YieldingContext {this}")
1717
: null;
1818

1919
if (LineNum.HasValue)

Code/ContextSystem/Contexter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ List<Context> contexts
6666
{
6767
if (statementStack.Count == 0)
6868
{
69-
return rs + $"{context.Name} expected to be inside a statement, but it isn't.";
69+
return rs + $"{context} expected to be inside a statement, but it isn't.";
7070
}
7171

7272
if (rqsContext.AcceptStatement(statementStack.Peek()).HasErrored(out var asError))

Code/ContextSystem/Contexts/Control/ElifStatementContext.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public class ElifStatementContext : StatementContext, IStatementExtender, IExten
2525
private readonly List<BaseToken> _condition = [];
2626

2727
private NumericExpressionReslover.CompiledExpression _expression;
28-
28+
29+
protected override string FriendlyName => "'elif' statement";
30+
2931
public override TryAddTokenRes TryAddToken(BaseToken token)
3032
{
3133
_condition.Add(token);
@@ -51,12 +53,12 @@ protected override IEnumerator<float> Execute()
5153
{
5254
if (_expression.Evaluate().HasErrored(out var error, out var objResult))
5355
{
54-
throw new ScriptRuntimeError(error);
56+
throw new ScriptRuntimeError(this, error);
5557
}
5658

5759
if (objResult is not bool result)
5860
{
59-
throw new ScriptRuntimeError($"An elif statement condition must evaluate to a boolean value, but received {objResult.FriendlyTypeName()}");
61+
throw new ScriptRuntimeError(this, $"An elif statement condition must evaluate to a boolean value, but received {objResult.FriendlyTypeName()}");
6062
}
6163

6264
if (!result)

Code/ContextSystem/Contexts/Control/ElseStatementContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class ElseStatementContext : StatementContext, IStatementExtender, IKeywo
1616

1717
public IExtendableStatement.Signal Extends => IExtendableStatement.Signal.DidntExecute;
1818

19+
protected override string FriendlyName => "'else' statement";
20+
1921
public override TryAddTokenRes TryAddToken(BaseToken token)
2022
{
2123
return TryAddTokenRes.Error("There should be no arguments after `else` keyword");

Code/ContextSystem/Contexts/Control/EndStatementContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ public class EndStatementContext : StandardContext, IKeywordContext
1111
public string KeywordName => "end";
1212
public string Description => "Ends the current statement's body.";
1313
public string[] Arguments => [];
14-
14+
15+
protected override string FriendlyName => "'end' keyword";
16+
1517
public override TryAddTokenRes TryAddToken(BaseToken token)
1618
{
1719
return TryAddTokenRes.Error("There can't be anything else on the same line as the 'end' keyword.");

Code/ContextSystem/Contexts/Control/IfStatementContext.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class IfStatementContext : StatementContext, IExtendableStatement, IKeywo
2222

2323
private NumericExpressionReslover.CompiledExpression _expression;
2424

25+
protected override string FriendlyName => "'if' statement";
26+
2527
public override TryAddTokenRes TryAddToken(BaseToken token)
2628
{
2729
_condition.Add(token);
@@ -47,12 +49,12 @@ protected override IEnumerator<float> Execute()
4749
{
4850
if (_expression.Evaluate().HasErrored(out var error, out var objResult))
4951
{
50-
throw new ScriptRuntimeError(error);
52+
throw new ScriptRuntimeError(this, error);
5153
}
5254

5355
if (objResult is not bool result)
5456
{
55-
throw new ScriptRuntimeError($"An if statement condition must evaluate to a boolean value, but received {objResult.FriendlyTypeName()}");
57+
throw new ScriptRuntimeError(this, $"An if statement condition must evaluate to a boolean value, but received {objResult.FriendlyTypeName()}");
5658
}
5759

5860
if (!result)

0 commit comments

Comments
 (0)