Skip to content
Snippets Groups Projects
Verified Commit 78f8c0d3 authored by Alexander Olofsson's avatar Alexander Olofsson
Browse files

Improve request error handling

parent 00fff23d
No related branches found
No related tags found
No related merge requests found
......@@ -29,9 +29,22 @@ module LiudeskCMDB
class RequestError < Error
attr_reader :code, :errors
def self.create(body, code)
case code
when "404"
NotFoundError.new body, code
else
RequestError.new body, code
end
end
def initialize(body, code)
@code = code
@errors = body["errors"] || body[:errors]
@errors = if body.is_a? String
[{ message: body }]
else
body["errors"] || body[:errors]
end
super message
end
......@@ -45,6 +58,8 @@ module LiudeskCMDB
end
end
class NotFoundError < RequestError; end
# Data models
module Models
def self.const_missing(const)
......
......@@ -75,7 +75,7 @@ module LiudeskCMDB
response.body
rescue Net::HTTPClientException
body = JSON.parse(response.body)
raise LiudeskCMDB::RequestError, body, response.code
raise LiudeskCMDB::RequestError.create(body, response.code)
end
def pretty_print_instance_variables
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment