wwauth/Google.Solutions.WWAuth/Util/ExceptionExtensions.cs (35 lines of code) (raw):
//
// Copyright 2022 Google LLC
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
using System;
using System.Reflection;
using System.Text;
namespace Google.Solutions.WWAuth.Util
{
/// <summary>
/// Extension methods for exceptions.
/// </summary>
public static class ExceptionExtensions
{
public static Exception Unwrap(this Exception e)
{
if (e is AggregateException aggregate)
{
e = aggregate.InnerException;
}
if (e is TargetInvocationException target)
{
e = target.InnerException;
}
return e;
}
public static string FullMessage(this Exception exception)
{
var fullMessage = new StringBuilder();
for (var ex = exception; ex != null; ex = ex.InnerException)
{
if (fullMessage.Length > 0)
{
fullMessage.Append(": ");
}
fullMessage.Append(ex.Message);
}
return fullMessage.ToString();
}
}
}