public void ErrorHandler(object sender, ErrorInformation errorInformation) { var cmdlet = _cmdlet; if (cmdlet == null) { return; } var exceptionName = errorInformation.Exception.InnerException?.GetType().Name ?? errorInformation.Exception.GetType().Name; var errorMessage = $"Perry ({exceptionName}): {errorInformation.ErrorMessage}"; if (_options?.IncludeVariable == true) { var variables = VariableParser.GetVariables(errorInformation.ErrorMessage); foreach (var variable in variables) { var variableValue = cmdlet.GetVariableValue(variable).ToString(); errorMessage += $"{Environment.NewLine} Variable: ${variable} = {variableValue}"; } } if (_options?.IncludeException == true) { errorMessage += $"{Environment.NewLine}{errorInformation.Exception}"; } cmdlet.WriteWarning(errorMessage); }
public void GetNoVariable() { var inputData = "Cause-Error -Input 'Oops'"; var result = VariableParser.GetVariables(inputData); Assert.Empty(result); }
public void GetMultipleVariables() { var inputData = "$var1 / $var2"; var result = VariableParser.GetVariables(inputData); Assert.Equal(new[] { "var1", "var2" }, result); }
public void GetVariableWithoutNamespace() { var inputData = "10 / $myvariable"; var result = VariableParser.GetVariables(inputData).Single(); Assert.Equal("myvariable", result); }
public void ErrorHandler(object sender, ErrorInformation errorInformation) { var cmdlet = _cmdlet; if (cmdlet == null || _options == null) { return; } var exceptionName = errorInformation.Exception.InnerException?.GetType().Name ?? errorInformation.Exception.GetType().FullName; var errorMessage = $"{DateTime.UtcNow:u} {exceptionName}{Environment.NewLine}{errorInformation.ErrorMessage}"; if (_options?.IncludeVariable == true) { var variables = VariableParser.GetVariables(errorInformation.ErrorMessage); foreach (var variable in variables) { var variableValue = cmdlet.GetVariableValue(variable).ToString(); errorMessage += $"{Environment.NewLine} Variable: ${variable} = {variableValue}"; } } if (_options?.IncludeException == true) { errorMessage += $"{Environment.NewLine}{errorInformation.Exception}"; } var outputFile = Path.Combine(_options.LogPath, "perry.log"); lock (_syncLogs) { try { RollLogFiles(outputFile); File.AppendAllText(outputFile, $"{errorMessage}{Environment.NewLine}", Encoding.UTF8); } catch (Exception e) { Trace.TraceError(e.ToString()); } } }