Skip to content

Commit 38695fb

Browse files
committed
Ensure we log the actual Win32 error on failure
Ensure we are reporting the actual `Win32Exception` error message when a failure to access the Windows credential store occurs. Also print all inner exceptions and any codes for `Win32Exception`s.
1 parent d70115b commit 38695fb

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

common/src/Microsoft.Git.CredentialManager/Application.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33
using System;
4+
using System.ComponentModel;
45
using System.Threading.Tasks;
56
using Microsoft.Git.CredentialManager.Commands;
7+
using Microsoft.Git.CredentialManager.SecureStorage;
68

79
namespace Microsoft.Git.CredentialManager
810
{
@@ -75,12 +77,23 @@ protected override void Dispose(bool disposing)
7577
base.Dispose(disposing);
7678
}
7779

78-
protected bool WriteException(Exception e)
80+
protected bool WriteException(Exception ex)
7981
{
80-
Context.StdError.WriteLine("fatal: {0}", e.Message);
81-
if (e.InnerException != null)
82+
// Try and use a nicer format for some well-known exception types
83+
switch (ex)
8284
{
83-
Context.StdError.WriteLine("fatal: {0}", e.InnerException.Message);
85+
case Win32Exception w32Ex:
86+
Context.StdError.WriteLine("fatal: {0} [0x{1:x}]", w32Ex.Message, w32Ex.NativeErrorCode);
87+
break;
88+
default:
89+
Context.StdError.WriteLine("fatal: {0}", ex.Message);
90+
break;
91+
}
92+
93+
// Recurse to print all inner exceptions
94+
if (!(ex.InnerException is null))
95+
{
96+
WriteException(ex.InnerException);
8497
}
8598

8699
return true;

common/src/Microsoft.Git.CredentialManager/SecureStorage/NativeMethods.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static void ThrowIfError(int error, string defaultErrorMessage = null)
4141
default:
4242
// The Win32Exception constructor will automatically get the human-readable
4343
// message for the error code.
44-
throw new Win32Exception(error, defaultErrorMessage);
44+
throw new Exception(defaultErrorMessage, new Win32Exception(error));
4545
}
4646
}
4747

0 commit comments

Comments
 (0)