Skip to content

Template engine throws some unexpected exceptions #282

Closed

Description

@Metalnem

Template engine can throw many unexpected exceptions when compiling or rendering templates (I assume that the expected exception type is HandlebarsException). Here are the types of exceptions it can throw, the code snippets used to trigger them, and the stack traces:

ArgumentException

HandlebarsDotNet.Handlebars.Compile("{{p=\np=}}")(new object());
Exception has occurred: CLR/System.ArgumentException
An unhandled exception of type 'System.ArgumentException' occurred in System.Private.CoreLib.dll: 'An item with the same key has already been added. Key: p'
   at System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException[T](T key)
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at HandlebarsDotNet.Compiler.HashParametersConverter.AccumulateParameters(IEnumerator`1 enumerator)
   at HandlebarsDotNet.Compiler.HashParametersConverter.<ConvertTokens>d__2.MoveNext()
   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at HandlebarsDotNet.Compiler.ExpressionBuilder.ConvertTokensToExpressions(IEnumerable`1 tokens)
   at HandlebarsDotNet.Compiler.HandlebarsCompiler.Compile(TextReader source)
   at HandlebarsDotNet.Handlebars.HandlebarsEnvironment.Compile(String template)

InvalidCastException

HandlebarsDotNet.Handlebars.Compile("{{>r=}}")(new object());
Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'HandlebarsDotNet.Compiler.HashParameterDictionary' to type 'System.String'.
   at lambda_method(Closure , TextWriter , Object )
   at HandlebarsDotNet.Handlebars.HandlebarsEnvironment.<>c__DisplayClass7_0.<Compile>b__0(Object context)

InvalidOperationException

HandlebarsDotNet.Handlebars.Compile("{{0")(new object());
Exception has occurred: CLR/System.InvalidOperationException
An unhandled exception of type 'System.InvalidOperationException' occurred in Handlebars.dll: 'Reached end of template before the expression was closed.'
   at HandlebarsDotNet.Compiler.Lexer.LiteralParser.AccumulateLiteral(TextReader reader, Boolean captureDelimiter, Char[] delimiters)
   at HandlebarsDotNet.Compiler.Lexer.LiteralParser.Parse(TextReader reader)
   at HandlebarsDotNet.Compiler.Lexer.Tokenizer.<Parse>d__8.MoveNext()
   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at HandlebarsDotNet.Compiler.HandlebarsCompiler.Compile(TextReader source)
   at HandlebarsDotNet.Handlebars.HandlebarsEnvironment.Compile(String template)

NullReferenceException

HandlebarsDotNet.Handlebars.Compile("{{@..}}")(new object());
Exception has occurred: CLR/System.NullReferenceException
An unhandled exception of type 'System.NullReferenceException' occurred in Handlebars.dll: 'Object reference not set to an instance of an object.'
   at HandlebarsDotNet.Compiler.PathBinder.ResolvePath(BindingContext context, String path)
   at HandlebarsDotNet.Handlebars.HandlebarsEnvironment.<>c__DisplayClass7_0.<Compile>b__0(Object context)

Found via SharpFuzz.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions