Skip to content

Commit 2399751

Browse files
committed
trace2: add exceptions
Add exceptions for which TRACE2 tracing can optionally be used. We choose to add these exceptions to ease the experience of writing TRACE2 error information - instead of having to add a call to Trace2's WriteError() message before every exception of interest, one can instead just use the appropriate Trace2 exception.
1 parent cab3b7a commit 2399751

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

src/shared/Core/Trace2Exception.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.IO;
4+
using GitCredentialManager.Authentication.OAuth;
5+
using GitCredentialManager.Interop;
6+
7+
namespace GitCredentialManager;
8+
9+
public class Trace2Exception : Exception
10+
{
11+
public Trace2Exception(ITrace2 trace2, string message) : base(message)
12+
{
13+
trace2.WriteError(message);
14+
}
15+
16+
public Trace2Exception(ITrace2 trace2, string message, string messageFormat) : base(message)
17+
{
18+
trace2.WriteError(message, messageFormat);
19+
}
20+
}
21+
22+
public class Trace2InvalidOperationException : InvalidOperationException
23+
{
24+
public Trace2InvalidOperationException(ITrace2 trace2, string message) : base(message)
25+
{
26+
trace2.WriteError(message);
27+
}
28+
}
29+
30+
public class Trace2OAuth2Exception : OAuth2Exception
31+
{
32+
public Trace2OAuth2Exception(ITrace2 trace2, string message) : base(message)
33+
{
34+
trace2.WriteError(message);
35+
}
36+
37+
public Trace2OAuth2Exception(ITrace2 trace2, string message, string messageFormat) : base(message)
38+
{
39+
trace2.WriteError(message, messageFormat);
40+
}
41+
}
42+
43+
public class Trace2InteropException : InteropException
44+
{
45+
public Trace2InteropException(ITrace2 trace2, string message, int errorCode) : base(message, errorCode)
46+
{
47+
trace2.WriteError($"message: {message} error code: {errorCode}");
48+
}
49+
50+
public Trace2InteropException(ITrace2 trace2, string message, Win32Exception ex) : base(message, ex)
51+
{
52+
trace2.WriteError(message);
53+
}
54+
}
55+
56+
public class Trace2GitException : GitException
57+
{
58+
public Trace2GitException(ITrace2 trace2, string message, int errorCode, string gitMessage) :
59+
base(message, gitMessage, errorCode)
60+
{
61+
var format = $"message: '{message}' error code: '{errorCode}' git message: '{{0}}'";
62+
var traceMessage = string.Format(format, gitMessage);
63+
64+
trace2.WriteError(traceMessage, format);
65+
}
66+
}
67+
68+
public class Trace2FileNotFoundException : FileNotFoundException
69+
{
70+
public Trace2FileNotFoundException(ITrace2 trace2, string message, string messageFormat, string fileName) :
71+
base(message, fileName)
72+
{
73+
trace2.WriteError(message, messageFormat);
74+
}
75+
}

0 commit comments

Comments
 (0)