I have a nuget package that does some stuff. In particular this one I call a HttpService here is the interface:
public interface IHttpService
{
Task<object> DeleteAsync(..);
Task<object> GetAsync(..);
Task<string> GetAccessTokenAsync(...);
Task<object> PatchAsync(...);
Task<object> PostAsync(..);
Task<object> PutAsync(..);
}
So, let's say an operation fails, say PatchAsync should I return null or should I throw new Exception(... what is the rule of thumb when my service fails. Specifically in my case, let's say we have this HttpService thing and it should make httpRequests based of some inputs. Cool.
Let's say it fails before actually making the request. Like, some preceding step before: var httpRequestResponse = await _httpClient.SendAsync(request);
Ok. So what is the rule. Idk, anything fails, and I check and say:
if(someVariable != null){
// everything is ok,
HappyPath();
}
return null;// ???
or
throw new Exception("Somthing went wrong"); // ???
public async Task<object> PatchAsync(string url,
object data,
string accessToken,
string userName,
Dictionary<string, string>? requestHeadersDictionary = null)
{
var serializedPayload = JsonConvert.SerializeObject(data);
var jsonPatchDocument = JsonConvert.DeserializeObject<JsonPatchDocument>(serializedPayload);
if(jsonPatchDocument != null)
{
return await PatchAsync(url, jsonPatchDocument, accessToken, userName, requestHeadersDictionary);
}
return null;
// TODO: How should I handle this? Fail silently? ... or
// return new Exception("Error with payload");
}
What should the consumer expect to happen? If you had built some app, and want to use this quick tool to handle all Http stuff, and something fails and its not the actual http request, what would you expect??? null or and Exception being thrown?