88using SER . Code . TokenSystem . Tokens . VariableTokens ;
99using SER . Code . ValueSystem ;
1010using SER . Code . VariableSystem . Bases ;
11- using SER . Code . VariableSystem . Variables ;
1211
1312namespace SER . Code . ContextSystem . Contexts . Control ;
1413
1514[ UsedImplicitly ]
1615public class OnErrorStatement : StatementContext , IStatementExtender , IKeywordContext , IAcceptOptionalVariableDefinitionsContext
1716{
1817 public string KeywordName => "on_error" ;
19- public string Description => "Catches an exception thrown inside of a " +
20- typeof ( AttemptStatement ) . FriendlyTypeName ( true ) ;
18+ public string Description =>
19+ $ "Catches an exception thrown inside of a { typeof ( AttemptStatement ) . FriendlyTypeName ( true ) } ";
20+
2121 public string [ ] Arguments => [ ] ;
22- public string ? Example =>
22+ public string Example =>
2323 """
2424 &collection = EmptyCollection
2525 attempt
2626 Print {CollectionFetch &collection 2}
27- # throws because there's nothing at index 2
27+ # ERROR: there's nothing at index 2
2828 on_error
29- with *exception
29+ with $message
3030
31- Print "Error!: {ExceptionInfo *exception message}"
31+ # this will print the error message
32+ Print "Error: {$message}"
3233 end
3334 """ ;
3435
@@ -45,8 +46,7 @@ public Exception? Exception
4546 field = value ;
4647 }
4748 }
48- private VariableToken ? _variableToken ;
49- private Variable ? _variable ;
49+ private LiteralVariableToken ? _variableToken ;
5050
5151 public override TryAddTokenRes TryAddToken ( BaseToken token )
5252 {
@@ -62,29 +62,29 @@ public Result SetOptionalVariables(params VariableToken[] variableTokens)
6262 {
6363 if ( variableTokens . Length > 1 )
6464 return $ "Too many arguments provided for { FriendlyName } , only 1 is allowed.";
65- var token = variableTokens . FirstOrDefault ( ) ;
66- if ( token is null ) return true ;
6765
68- if ( token is not ReferenceVariableToken )
69- return $ "Variable { token . RawRepr } cannot be used for a { FriendlyName } as it's not a " +
70- $ "{ typeof ( ReferenceVariable ) . FriendlyTypeName ( true ) } .";
66+ if ( variableTokens . First ( ) is not LiteralVariableToken token )
67+ {
68+ return $ "{ FriendlyName } only accepts a literal variable.";
69+ }
7170
7271 _variableToken = token ;
7372 return true ;
7473 }
7574
7675 protected override IEnumerator < float > Execute ( )
7776 {
77+ Variable ? variable = null ;
7878 if ( _variableToken is not null )
7979 {
80- _variable = Variable . Create ( _variableToken . Name , Value . Parse ( Exception ! , Script ) ) ;
81- Script . AddLocalVariable ( _variable ) ;
80+ variable = Variable . Create ( _variableToken . Name , Value . Parse ( Exception ! . Message , Script ) ) ;
81+ Script . AddLocalVariable ( variable ) ;
8282 }
8383
8484 var coro = RunChildren ( ) ;
8585 while ( coro . MoveNext ( ) )
8686 yield return coro . Current ;
8787
88- if ( _variable is not null ) Script . RemoveLocalVariable ( _variable ) ;
88+ if ( variable is not null ) Script . RemoveLocalVariable ( variable ) ;
8989 }
9090}
0 commit comments