Skip to content

Commit

Permalink
Nil friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
m-sadegh-sh committed Mar 17, 2024
1 parent 5539773 commit 280cacd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
27 changes: 16 additions & 11 deletions Olive/-Extensions/Exception.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ public static string ToFullMessage(this Exception @this, string additionalMessag
continue;
}

resultBuilder.AppendLine(err.Message);
// NSErrorException.Message may result in a null reference exception!
try { resultBuilder.AppendLineIf(err.Message); }
catch { }

if (includeData && err.Data != null && err.Data.Count > 0)
if (includeData && err.Data?.Count > 0)
{
resultBuilder.AppendLine("\r\nException Data:\r\n{");

Expand All @@ -123,9 +125,9 @@ public static string ToFullMessage(this Exception @this, string additionalMessag
resultBuilder.AppendLine("}");
}

if (err is ReflectionTypeLoadException)
if (err is ReflectionTypeLoadException refErr)
{
foreach (var loaderEx in (err as ReflectionTypeLoadException).LoaderExceptions)
foreach (var loaderEx in refErr.LoaderExceptions)
resultBuilder.AppendLine("Type load exception: " + loaderEx.ToFullMessage());
}

Expand All @@ -142,13 +144,16 @@ public static string ToFullMessage(this Exception @this, string additionalMessag
}
}

var stack = @this.GetUsefulStack().TrimOrEmpty();

if (includeStackTrace && stack.HasValue())
if (includeStackTrace)
{
var stackLines = stack.ToLines();
stackLines = stackLines.Except(l => l.Trim().StartsWith("at System.Data.")).ToArray();
resultBuilder.AppendLine(stackLines.ToString("\r\n\r\n").WithPrefix("\r\n--------------------------------------\r\nSTACK TRACE:\r\n\r\n"));
var stack = @this.GetUsefulStack().TrimOrEmpty();

if (stack.HasValue())
{
var stackLines = stack.ToLines();
stackLines = stackLines.Except(l => l.Trim().StartsWith("at System.Data.")).ToArray();
resultBuilder.AppendLine(stackLines.ToString("\r\n\r\n").WithPrefix("\r\n--------------------------------------\r\nSTACK TRACE:\r\n\r\n"));
}
}

return resultBuilder.ToString();
Expand All @@ -159,7 +164,7 @@ public static string GetUsefulStack(this Exception @this)
if (@this.InnerException == null) return @this.StackTrace;

if (@this is TargetInvocationException || @this is AggregateException)
return @this.InnerException.GetUsefulStack();
return @this.InnerException?.GetUsefulStack() ?? @this.StackTrace;

return @this.StackTrace;
}
Expand Down
2 changes: 1 addition & 1 deletion Olive/Olive.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.1.361.0</Version>
<Version>2.1.362.0</Version>
<OutputType>Library</OutputType>
<LangVersion>latest</LangVersion>
<!--<PackageId>Olive</PackageId>-->
Expand Down

0 comments on commit 280cacd

Please sign in to comment.