|
| 1 | +using CodeFactory.WinVs.Models.CSharp.Builder; |
| 2 | +using Microsoft.Extensions.Logging; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace CodeFactory.Automation.NDF.Logic.AspNetCore.Blazor |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// CodeBlock that builds a catch block for a standard Exception and returns a NDF managed exception. |
| 13 | + /// </summary> |
| 14 | + public class CatchBlockExceptionBlazorControllerMessage:BaseCatchBlock |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Log level used for catch block log messages. |
| 18 | + /// </summary> |
| 19 | + private readonly LogLevel _logLevel; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Name of the method that will display the error message. |
| 23 | + /// </summary> |
| 24 | + private readonly string _showErrorMessageName; |
| 25 | + |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Creates a instances of the <see cref="CatchBlockExceptionNDF"/> |
| 29 | + /// </summary> |
| 30 | + /// <param name="loggerBlock">Optional, logger block to use for logging in the catch block.</param> |
| 31 | + /// <param name="logLevel">Optional, sets the level for log messages default is Error.</param> |
| 32 | + /// <param name="showErrorMethodName">Default name of the method to call from the controller when raising the message from a NDF managed exception, default is 'ShowErrorMessage'</param> |
| 33 | + public CatchBlockExceptionBlazorControllerMessage(ILoggerBlock loggerBlock = null,LogLevel logLevel = LogLevel.Error, string showErrorMethodName = null) : base(loggerBlock) |
| 34 | + { |
| 35 | + _showErrorMessageName = showErrorMethodName != null |
| 36 | + ? showErrorMethodName |
| 37 | + : "ShowErrorMessage"; |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary>Builds the catch block</summary> |
| 41 | + /// <param name="syntax">Syntax to be injected into the catch block, optional parameter.</param> |
| 42 | + /// <param name="multipleSyntax">Multiple syntax statements has been provided to be used by the catch block,optional parameter.</param> |
| 43 | + /// <param name="memberName">Optional parameter that determines the target member the catch block is implemented in.</param> |
| 44 | + /// <returns>Returns the generated catch block</returns> |
| 45 | + protected override string BuildCatchBlock(string syntax = null, IEnumerable<NamedSyntax> multipleSyntax = null, string memberName = null) |
| 46 | + { |
| 47 | + SourceFormatter formatter = new SourceFormatter(); |
| 48 | + |
| 49 | + formatter.AppendCodeLine(0,"catch (Exception unhandledException)"); |
| 50 | + formatter.AppendCodeLine(0,"{"); |
| 51 | + if (LoggerBlock != null) |
| 52 | + { |
| 53 | + formatter.AppendCodeLine(1, LoggerBlock.GenerateLogging(LogLevel.Error, "The following unhandled exception occurred, see exception details. Throwing a unhandled managed exception.",false,"unhandledException") ); |
| 54 | + formatter.AppendCodeLine(1, LoggerBlock.GenerateExitLogging(_logLevel, memberName)); |
| 55 | + } |
| 56 | + formatter.AppendCodeLine(1,"var unhandledError = new UnhandledException();"); |
| 57 | + formatter.AppendCodeLine(1,$"{_showErrorMessageName}(unhandledError.Message);"); |
| 58 | + formatter.AppendCodeLine(0,"}"); |
| 59 | + |
| 60 | + return formatter.ReturnSource(); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments