/// <summary> /// Throw exception. /// </summary> /// <param name="ctx"></param> /// <param name="e">Represents errors that occur during application execution.</param> /// <param name="tag">Represents errors tag.</param> /// <param name="writer">See the <see cref="System.IO.TextWriter"/>.</param> public static async Task ThrowExceptionAsync(this TemplateContext ctx, TemplateException e, ITag tag, System.IO.TextWriter writer) { ctx.AddError(e); if (!ctx.ThrowExceptions) { await writer.WriteAsync(tag.ToSource()); } }
/// <summary> /// Performs the render for a tags. /// </summary> /// <param name="ctx"></param> /// <param name="writer">See the <see cref="System.IO.TextWriter"/>.</param> /// <param name="collection">The tags collection.</param> /// <param name="cancellationToken">See the <see cref="CancellationToken"/>.</param> public static async Task RenderAsync(this TemplateContext ctx, System.IO.TextWriter writer, ITag[] collection, CancellationToken cancellationToken = default) { if (writer == null) { throw new ArgumentNullException("\"writer\" cannot be null."); } if (collection != null && collection.Length > 0) { for (int i = 0; i < collection.Length; i++) { try { var tagResult = Execute(ctx, collection[i]); if (tagResult != null) { if (tagResult is Task task) { var type = tagResult.GetType(); if (type.IsGenericType) { var taskResult = (Task <string>) typeof(Utility).CallGenericMethod(null, "ExcuteTaskAsync", type.GetGenericArguments(), tagResult); var taskValue = await taskResult; await writer.WriteAsync(taskValue); } else { await task; } continue; } await writer.WriteAsync(tagResult.ToString()); } } catch (TemplateException e) { await ThrowExceptionAsync(ctx, e, collection[i], writer); } catch (System.Exception e) { var baseException = e.GetBaseException(); await ThrowExceptionAsync(ctx, new ParseException(collection[i], baseException), collection[i], writer); } } } }
private void write(string title, IList <Diagnostic> errors) { if (errors.Count > 0) { writer.WriteAsync(errors.Count.ToString()); } else { writer.WriteAsync("No"); } writer.WriteAsync(" error"); if (errors.Count > 1) { writer.WriteAsync('s'); } writer.WriteAsync(" in \""); writer.WriteAsync(title); if (errors.Count > 0) { writer.WriteLineAsync("\":"); foreach (var e in errors) { writer.WriteLineAsync(e.ToString()); } } else { writer.WriteLineAsync("\"."); } }