lib/datacentred/error.rb in datacentred-0.1.1pre vs lib/datacentred/error.rb in datacentred-1.1.1
- old
+ new
@@ -1,28 +1,50 @@
module Datacentred
- class Error < StandardError
+ # Behaviours and exceptions for recoverable errors.
+ module Errors
+ # Test server response and raise appropriate error if an error has been returned.
+ #
+ # @raise [Error] Appropriate error for server response code.
+ # @return [nil] Returns nil on success
def self.raise_unless_successful(status, body)
return if status.to_s.start_with? "2" # 2xx
- err = Datacentred::errors[status]
+ err = errors[status]
message = body&.fetch("errors")&.first&.fetch("detail")
if err
- raise err, message
+ raise err, message || status.to_s
else
- raise Datacentred::Error, "Error #{status}: #{message}"
+ raise Error, "Error #{status}: #{message}"
end
end
- end
- class NotFoundError < StandardError; end
- class UnprocessableEntity < StandardError; end
- class Unauthorized < StandardError; end
+ # Datacentred base error
+ class Error < StandardError ; end
- private
+ # Raised when an entity cannot be located using the unique id specified.
+ #
+ # Corresponds to a HTTP 404 error.
+ class NotFound < Error; end
- def self.errors
- {
- 401 => Unauthorized,
- 404 => NotFoundError,
- 422 => UnprocessableEntity
- }
+ # Raised usually when data validations fail on operations that mutate state.
+ #
+ # Corresponds to a HTTP 422 error.
+ class UnprocessableEntity < Error; end
+
+ # Raised when credentials are invalid.
+ #
+ # Credentials may be invalid because they're incorrect, or because they correspond to an account
+ # that does not have the correct permissions to access the API.
+ #
+ # Corresponds to a HTTP 403 error.
+ class Unauthorized < Error; end
+
+ private
+
+ def self.errors
+ {
+ 401 => Datacentred::Errors::Unauthorized,
+ 404 => Datacentred::Errors::NotFound,
+ 422 => Datacentred::Errors::UnprocessableEntity
+ }
+ end
end
end