SharpScript Catch Exception

Hello,

I would like to catch an error when a variable cannot be converted into a number:

{{ someVariable |> toInt |> to => myInt }}

I am using web watch to run the template.

I tried with:

{{ someVariable |> toInt |> ifErrorReturn(0) |> to => myInt }}

and with:
{{ continueExecutingFiltersOnError }}

However, exception still throws.

Thank you.

I’ve updated #Script to handle covering more Exceptions in the latest v5.8.1 on MyGet.

When running a Sharp Script using the dotnet tools (i.e. x/app/web) it always displays the Last Exception if there was one so I’ve also updated the tools to be able to run in “Silent Mode” where it will ignore displaying the Last Exception, e.g:

$ x -q run script.ss

You can use any of the flag variants: -q, -quiet, --quiet, /q, /quiet,

Update dotnet tool with:

$ dotnet tool update -g x

Please note that ifErrorReturn is not a script method, it’s an instruction option for methods that implement managed Exceptions.

1 Like

Thanks for your support.

I did use “x watch code.ss” for testing purposes, and certainly it is not throwing error after using -q option.

But when I take the code to my application and try to render with:

output.Add(pageResult.RenderScript());

The exception is thrown and “output” will not contain the desired render.

What I am trying to achieve, is to do a conditional render by using #if blocks.
If variable can convert to number I will render something, if not, I will render a different thing.

Int variable initialization is done at the beginning of the template, that is why error is thrown.

Thank you very much.

Did you clear your NuGet cache before downloading the latest v5.8.1 packages on MyGet, if you did can you provide the C# + #Script code I can run that shows the issue?

I just cleared nuget packages and got same result.

Please try following code:

public static void Main(string[] args)
{
    var template = @"{{ continueExecutingFiltersOnError }}
```code
'h1' |> lower |> to => elemType
elemType |> toInt |> raw
```";

    var context = new ScriptContext().Init();
    var dynamicPage = context.OneTimePage(template);
    var pageResult = new PageResult(dynamicPage);
    var output = pageResult.RenderScript();
    Console.Write(output);
}

Ok it’s because the Exception is still associated with the PageResult and thrown at the end of the script so I’ve added a catchError method that will assign and swallow the Exception which you can use like:

    var template = @"{{ 'ex' |> catchError }}
```code
'h1' |> lower |> to => elemType
elemType |> toInt |> raw
```";

This change is available from the latest v5.8.1 that’s now available on MyGet.

1 Like

It worked.

Thank you very mcuh.

As an additional comment, I had to initialize elemTypeInt before possible exception, otherwise #if block throws another exception (in my case: “The given key ‘h5’ was not present in the dictionary.”).

```code
'ex' |> catchError
p.mainClass |> lower |> to => elemType
```

{{#if ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(elemType) }}
<Typography variant="{{elemType}}" className="{{p.classes}}" gutterBottom>
{{p.value}}
</Typography>
{{/if}}

{{ 0 |> to => elemTypeInt }}
{{ elemType |> toInt |> to => elemTypeInt }}
{{#if elemTypeInt and (elemTypeInt > 0 or elemTypeInt < 13)}}
<Grid item container className="{{p.classes}}" direction="column" {{p.attributes}}
{{ p.children |> raw }}
</Grid>
{{/if}}

But it is working OK now.

Thanks again.

1 Like

FYI there’s also an isInt you can use to test if a variable is an integer, e.g:

{{#if elemType.isInt() }}
  ...
{{/if}}

There’s a number of different type assertions you can test starting with `is*:

https://sharpscript.net/docs/scripts-reference?nameContains=is&tab=default-scripts

1 Like