Где моя вина?
У меня есть тип класса Exception
public class ApiException : Exception {
public ApiException(string message) : base(message) {
}
}
В некоторых ситуациях я звоню throw new ApiException("Message");
Например, здесь:
public static async Task<string> ValidateToken(string token) {
Dictionary<string, string> values = new Dictionary<string, string> {
{ "token", token}
};
FormUrlEncodedContent content = new FormUrlEncodedContent(values);
HttpResponseMessage response = await client.PostAsync(Globals.sso, content);
string responseString = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode) {
TokenResp result = JsonConvert.DeserializeObject<TokenResp>(responseString);
if (result.Token != token)
throw new ApiException("Token is invalid");
} else {
NonFieldResponse resp = JsonConvert.DeserializeObject<NonFieldResponse>(responseString);
string msg = null;
foreach (string message in resp.non_field_errors) {
if (msg != null) msg += ", ";
msg += message;
}
throw new ApiException(msg);
}
Где-то мне нужно catch
исключений, как здесь:
try {
Type = ValidateToken(token).Result;
} catch (ApiException ae) {
Console.WriteLine(ae.Message);
} catch (Exception e) {
Console.WriteLine(e.Message);
}
Но catch (ApiException ae)
не происходит, всегда простое Exception
(где e.GetType()
равно AggregateException
и e.InnerException.GetType()
is ApiException
).
Как отловить мое исключение?