<commit_before>﻿using System;
using System.Runtime.CompilerServices;
using Avalonia.Data;

namespace Avalonia.PropertyStore
{
    internal static class LoggingUtils
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static void LogIfNecessary(
            AvaloniaObject owner,
            AvaloniaProperty property,
            BindingNotification value)
        {
            if (value.ErrorType != BindingErrorType.None)
                Log(owner, property, value.Error!);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static void LogIfNecessary<T>(
            AvaloniaObject owner,
            AvaloniaProperty property,
            BindingValue<T> value)
        {
            if (value.HasError)
                Log(owner, property, value);
        }

        public static void LogInvalidValue(
            AvaloniaObject owner,
            AvaloniaProperty property,
            Type expectedType,
            object? value)
        {
            if (value is not null)
            {
                owner.GetBindingWarningLogger(property, null)?.Log(
                    owner,
                    "Error in binding to {Target}.{Property}: expected {ExpectedType}, got {Value} ({ValueType})",
                    owner,
                    property,
                    expectedType,
                    value,
                    value.GetType());
            }
            else
            {
                owner.GetBindingWarningLogger(property, null)?.Log(
                    owner,
                    "Error in binding to {Target}.{Property}: expected {ExpectedType}, got null",
                    owner,
                    property,
                    expectedType);
            }
        }

        private static void Log(
            AvaloniaObject owner,
            AvaloniaProperty property,
            Exception e)
        {
            owner.GetBindingWarningLogger(property, e)?.Log(
                owner,
                "Error in binding to {Target}.{Property}: {Message}",
                owner,
                property,
                e.Message);
        }

        private static void Log<T>(
            AvaloniaObject owner,
            AvaloniaProperty property,
            BindingValue<T> value)
        {
            owner.GetBindingWarningLogger(property, value.Error)?.Log(
                owner,
                "Error in binding to {Target}.{Property}: {Message}",
                owner,
                property,
                value.Error!.Message);
        }
    }
}<commit_msg>Unwrap exceptions before logging.<commit_after>﻿using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using Avalonia.Data;

namespace Avalonia.PropertyStore
{
    internal static class LoggingUtils
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static void LogIfNecessary(
            AvaloniaObject owner,
            AvaloniaProperty property,
            BindingNotification value)
        {
            if (value.ErrorType != BindingErrorType.None)
                Log(owner, property, value.Error!);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static void LogIfNecessary<T>(
            AvaloniaObject owner,
            AvaloniaProperty property,
            BindingValue<T> value)
        {
            if (value.HasError)
                Log(owner, property, value.Error!);
        }

        public static void LogInvalidValue(
            AvaloniaObject owner,
            AvaloniaProperty property,
            Type expectedType,
            object? value)
        {
            if (value is not null)
            {
                owner.GetBindingWarningLogger(property, null)?.Log(
                    owner,
                    "Error in binding to {Target}.{Property}: expected {ExpectedType}, got {Value} ({ValueType})",
                    owner,
                    property,
                    expectedType,
                    value,
                    value.GetType());
            }
            else
            {
                owner.GetBindingWarningLogger(property, null)?.Log(
                    owner,
                    "Error in binding to {Target}.{Property}: expected {ExpectedType}, got null",
                    owner,
                    property,
                    expectedType);
            }
        }

        private static void Log(
            AvaloniaObject owner,
            AvaloniaProperty property,
            Exception e)
        {
            if (e is TargetInvocationException t)
                e = t.InnerException!;

            if (e is AggregateException a)
            {
                foreach (var i in a.InnerExceptions)
                    Log(owner, property, i);
            }
            else
            {
                owner.GetBindingWarningLogger(property, e)?.Log(
                    owner,
                    "Error in binding to {Target}.{Property}: {Message}",
                    owner,
                    property,
                    e.Message);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.07592484989347278, 'fraction_numerical': 0.0, 'mean_word_length': 1.3022737405260811, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6533055', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1162, 'n_words': 284}