2

I am writing a C# client SDK to a REST Api. This SDK sends JSON-serialized models to the API, and implements a first level of verification of the parameters. If the verification fails, it throws an exception.

An example would be:

if (requiredParameter == null)
{
    throw new ApiException(400, "Missing required parameter");
}

As you can see, ApiException takes an integer as parameter which represents an HTTP error code (in that context "Bad Request"). I'm not sure HTTP codes are semantically meant to be used when there is no HTTP exchange being done between the client and the server (even though in that context going further would result in a 400 error code being replied by the server).

I would love to have some thoughts on this matter.

3
  • Why do you check the parameter on the client?
    – marstato
    Commented Oct 9, 2019 at 14:26
  • Because the client is written (automatically generated really) against an Open API json definition of the rest API and has therefore knowledge of required parameters of an action, which allows it to save a request doomed to failure by doing this verification.
    – Toto
    Commented Oct 9, 2019 at 14:33
  • I've considered wrapping library calls in a Result<T> object that has an HTTP-based integer code for categorizing the failure/success of the call.
    – GHP
    Commented Oct 9, 2019 at 16:46

1 Answer 1

5

This does seem odd, but I guess I can't be surprised these days where everyone thinks in terms of RESTfulness

I would expect the client to hide the underlying implementation, not fake expose it.

So if a parameter is missing throw a standard ArgumentException or have some custom validation method that you have to call before sending.

If the server returns a HTTP response code, intercept that, parse it and if it's part of a restful response translate into an appropriate error.

Only if its a legitimate error with the HTTP communication would I expect a HttpException to be thrown with a HTTP error code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.