Skip to content

Commit

Permalink
Handle client errors (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberdelia committed Jan 5, 2021
1 parent 2340ab9 commit d2efdfc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
20 changes: 16 additions & 4 deletions lib/td/client/api.rb
Expand Up @@ -636,15 +636,27 @@ def raise_error(msg, res, klass=nil)
klass
else
case status_code
when "400"
BadRequestError
when "401"
AuthError
when "403"
ForbiddenError
when "404"
NotFoundError
when "405"
MethodNotAllowedError
when "409"
message = "#{message}: conflicts_with job:#{error["details"]["conflicts_with"]}" if error["details"] && error["details"]["conflicts_with"]
AlreadyExistsError
when "401"
AuthError
when "403"
ForbiddenError
when "415"
UnsupportedMediaTypeError
when "422"
UnprocessableEntityError
when "429"
TooManyRequestsError
when /\A4\d\d\z/
ClientError
else
message = "#{status_code}: #{message}"
APIError
Expand Down
40 changes: 32 additions & 8 deletions lib/td/client/api_error.rb
Expand Up @@ -13,25 +13,49 @@ def initialize(error_message = nil, api_backtrace = nil)
end
end

# 401 API errors
class AuthError < APIError
# 4xx Client Errors
class ClientError < APIError
end

# 403 API errors, used for database permissions
class ForbiddenError < APIError
# 400 Bad Request
class BadRequestError < ClientError
end

# 409 API errors
class AlreadyExistsError < APIError
# 401 Unauthorized
class AuthError < ClientError
end

# 403 Forbidden, used for database permissions
class ForbiddenError < ClientError
end

# 404 Not Found
class NotFoundError < ClientError
end

# 405 Method Not Allowed
class MethodNotAllowedError < ClientError
end

# 409 Conflict
class AlreadyExistsError < ClientError
attr_reader :conflicts_with
def initialize(error_message = nil, api_backtrace = nil, conflicts_with=nil)
super(error_message, api_backtrace)
@conflicts_with = conflicts_with
end
end

# 404 API errors
class NotFoundError < APIError
# 415 Unsupported Media Type
class UnsupportedMediaTypeError < ClientError
end

# 422 Unprocessable Entity
class UnprocessableEntityError < ClientError
end

# 429 Too Many Requests
class TooManyRequestsError < ClientError
end

end

0 comments on commit d2efdfc

Please sign in to comment.