I'm trying to map different CryptographicExceptions to custom exceptions and messages. For example, "Object already exists" ==> "Not enough permissions to access an existing RSA key container". However, when I examine CryptographicException class, I don't find any error code collection like other exception types have. I'm running on 3.5, so HResult is not available either. Finally, I cannot rely on the message, since it can be localized. Any other ideas?
Asked
Active
Viewed 670 times
0
-
Sounds to me like you are trying to create something that is extremely brittle. If the implementation of either the exception or the throwing class only changes slightly, you won't get your customized message anymore. Generally, in managed languages, the reliance on wrapper classes should be kept to a minimum. – Maarten Bodewes Mar 12 '12 at 19:39
1 Answers
1
public Exception GetMappedCryptographicException(CryptographicException e)
{
uint hresult = (uint)Marshal.GetHRForException(e);
switch (hresult)
{
case 0x8009000F; // OBJECT_ALREADY_EXISTS
return new Exception(e, "Not enough permissions to access RSA key container.");
default:
return new Exception(e, "Unexpected cryptographic exception occurred.");
}
}