akamsteeg / AtleX.HaveIBeenPwned

A fully async .NET Standard client library for the API of HaveIBeenPwned.com
https://www.nuget.org/packages/AtleX.HaveIBeenPwned/
MIT License
5 stars 0 forks source link

Wrap InvalidOperationException in HaveIBeenPwnedClientException #71

Closed akamsteeg closed 1 year ago

akamsteeg commented 2 years ago

We strive to only throw three basic exception types:

  1. ArgumentException (with ArgumentNullException and c.s.)
  2. TaskCanceledException/OperationCanceledException when the cancellation token indicates the operation should be canceled
  3. HaveIBeenPwnedClientException for all internal exceptions including InvalidApiKeyException and RateLimitExceededException

This makes handling exceptions for any users of the library quite easy.

  1. When input is wrong, you'll get an ArgumentException
  2. When you want things to stop, you'll get an OperationCanceledException (TaskCanceledException)
  3. When something goes wrong with the request or the result you'll get a HaveIBeenPwnedClientException or a derivative.

Easy, at most three catches or of course just a single one if you don't care and want to go all Pokémon and catch them all.

But, we directly throw an InvalidOperationException here:

https://github.com/akamsteeg/AtleX.HaveIBeenPwned/blob/d23072b9aa1de60a6d60b9a334f3ca07d8625dfb/src/AtleX.HaveIBeenPwned/HaveIBeenPwnedClient.cs#L343-L346

That needs to change to a HaveIBeenPwnedClientException with possibly the InvalidOperationException wrapped in it.