I need to catch "The remote server returned an error: (401) Unauthorized." exception to do another action.
The problem is, I'm not sure how to recognize it.
Right now, I'm using
catch (Exception ex)
{
}
but the exception "The remote server returned an error: (401) Unauthorized." appears inside the "InnerException" and the "Message", so no status code or anything.
How can I track this specific exception?
I am using GoogleDrive SDK if it's relevant.
Tried to use this:
catch (GoogleApiRequestException e) {
if (e.HttpStatusCode == HttpStatusCode.Unauthorized) {
// Credentials have been revoked.
// TODO: Redirect the user to the authorization URL.
throw new NotImplementedException();
}
}
But e.HttpStatusCode was 0, so it didn't recognize it.
I guess I can use
if(ex.Message.Contains("401"))
But it's really bad