3 ms·
no, libraries should not throw exceptions if they get a http response. that is a perfect example where the programmer has to roll his own, because this is entir
by y4mi 10y ago
no, libraries should not throw exceptions if they get a http response. that is a perfect example where the programmer has to roll his own, because this is entirely situational.
the pseudo-code example of the grandparent seems to me to implement
a) detecting the http code,
b) throwing an exception and finally
c) handling this except.
- spion 10y agoWhich pseudo code are you referring to? Exceptions being for "exceptional" circumstances is a popular misconception. Exceptions are for "an upper layer to handle". You can use them for any alternative values you wish to propagate to the upper layer.
- paulddraper 10y ago> Exceptions are for "an upper layer to handle". So are return values :) The difference is that return value is for the next immediate layer; an exception is for some layer, but I don't know what. IMO your HTTP layer should not be throwing exceptions based on status code. The layer above that should.
- spion 10y agoOh ok. Its not pseudocode, the combinators work :) I would put it the other way around. The http layer should not decide that the layer immediately above it must be the one to handle all its errors. It should also be the one that defines the default (success) case and the error case, since those are defined by the HTTP protocol.
- vinylkey 10y agoSee superagent where anything that's not a 2xx is treated as an error (https://github.com/visionmedia/superagent/issues/283 https://github.com/visionmedia/superagent/issues/283)