Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper for adding data to an exception while throwing it #2

Closed
AArnott opened this issue Dec 8, 2016 · 0 comments
Closed

Add helper for adding data to an exception while throwing it #2

AArnott opened this issue Dec 8, 2016 · 0 comments
Assignees

Comments

@AArnott
Copy link
Member

AArnott commented Dec 8, 2016

The actual code is below, which could be used as follows.

            throw new ArgumentException("Overlapping spans").AddData(bufferSpans.Key, bufferSpans.Value[s], bufferSpans.Value[s - 1]);
// or
            throw new ArgumentException("Overlapping spans").Modify((e) => e.Data.Add("bufferSpans", bufferSpans));
    public static class ExceptionsWithData
    {
        /// <summary>
        /// Add <paramref name="data"/> to the Data member of <paramref name="e"/> before returning the modified exception.
        /// </summary>
        /// <remarks>
        /// <para>This method should be used to add context (beyond the message and callstack we normally get) to the exception
        /// that would be useful when debugging Watson crashes.</para>
        /// <para>Do not use this method when you expect the exception to be handled.</para>
        /// </remarks>
        public static T AddData<T>(this T e, params object[] data) where T : Exception
        {
            if (data.Length > 0)
            {
                e.Data.Add("AddData", data);
            }
 
            return e;
        }
 
        /// <summary>
        /// Modify <paramref name="e"/> by performing <paramref name="action"/> on it before returning the modified exception.
        /// </summary>
        /// <remarks>
        /// <para>This method should be used to add context (beyond the message and callstack we normally get) to the exception
        /// that would be useful when debugging Watson crashes.</para>
        /// <para>Do not use this method when you expect the exception to be handled.</para>
        /// </remarks>
        public static T Modify<T>(this T e, Action<T> action) where T : Exception
        {
            if (action != null)
            {
                action(e);
            }
 
            return e;
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant