Skip to content

Commit

Permalink
Error handling changes
Browse files Browse the repository at this point in the history
  • Loading branch information
skyguy94 committed Jun 3, 2015
1 parent 351ee75 commit 38b0bd8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
17 changes: 15 additions & 2 deletions poshring/Credential.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -83,8 +84,20 @@ public void Save()
var result = UnsafeAdvapi32.CredWriteW(ref _nativeCredential, 0);
if (!result)
{
var error = (CredentialErrors) Marshal.GetLastWin32Error();
throw new CredentialManagerException(error, "Could not save credential.");
var error = (CredentialErrors)Marshal.GetLastWin32Error();
switch (error)
{
case CredentialErrors.Success:
case CredentialErrors.NotFound:
break;
case CredentialErrors.BadUsername:
throw new InvalidOperationException("UserName was not specified correctly.");
case CredentialErrors.NoSuchLogonSession:
case CredentialErrors.InvalidFlags:
throw new Win32Exception((int)error);
default:
throw new InvalidOperationException("Unexpected error saving credential.");
}
}
}

Expand Down
14 changes: 13 additions & 1 deletion poshring/CredentialsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public IEnumerable<Credential> GetCredentials()
var error = (CredentialErrors) Marshal.GetLastWin32Error();
switch (error)
{
case CredentialErrors.Success:
break;
case CredentialErrors.NotFound:
return Enumerable.Empty<Credential>();
case CredentialErrors.NoSuchLogonSession:
Expand Down Expand Up @@ -52,7 +54,17 @@ public void DeleteCredential(Credential credential)
if (!UnsafeAdvapi32.CredDeleteW(credential.TargetName, credential.Type, 0))
{
var error = (CredentialErrors)Marshal.GetLastWin32Error();
throw new CredentialManagerException(error, "Could not delete credential.");
switch (error)
{
case CredentialErrors.Success:
case CredentialErrors.NotFound:
break;
case CredentialErrors.NoSuchLogonSession:
case CredentialErrors.InvalidFlags:
throw new Win32Exception((int)error);
default:
throw new InvalidOperationException("Unexpected error while removing credential.");
}
}
}
}
Expand Down

0 comments on commit 38b0bd8

Please sign in to comment.