messagebird / ruby-rest-api

MessageBird's REST API for Ruby
BSD 2-Clause "Simplified" License
37 stars 46 forks source link

Server errors raised as InvalidPhoneNumberException #17

Closed epels closed 4 years ago

epels commented 6 years ago

Currently, if the MessageBird API returns a unexpected HTTP status code, the client raises an error.

case response.code.to_i
when 200, 201, 204, 401, 404, 405, 422
    json = JSON.parse(response.body)
else
    raise InvalidPhoneNumberException, 'Unknown response from server'
end

This is always an InvalidPhoneNumberException. It'd make more sense to have a more subtle name for this, e.g. ServerException.

Additionally, InvalidPhoneNumberException derives from TypeError: as per the docs, this is an error type that should be raised "when encountering an object that is not of the expected type".


It should be noted that this is a backwards incompatible change, and therefore requires a new major version. This is not currently planned.

A temporary backwards compatible workaround is to create a new exception type that extends the current InvalidPhoneNumberException:

class ServerException < InvalidPhoneNumberException; end