Skip to content

Commit

Permalink
support for error metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Belo committed Sep 16, 2015
1 parent b0d8991 commit 06766df
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/pliny/errors.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
module Pliny
module Errors
class Error < StandardError
attr_accessor :id
attr_accessor :id, :metadata

def self.render(error)
headers = { "Content-Type" => "application/json; charset=utf-8" }
data = { id: error.id, message: error.message }
data = { id: error.id, message: error.message }.merge(error.metadata)
[error.status, headers, [MultiJson.encode(data)]]
end

def initialize(options={})
@id = options[:id]
@metadata = options[:metadata] || {}
super(options[:message])
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
e = Pliny::Errors::Error.new(id: :fail)
assert_equal :fail, e.id
end

it "takes metadata" do
meta = { resource: "artists" }
e = Pliny::Errors::Error.new(metadata: meta)
assert_equal meta, e.metadata
end
end

describe Pliny::Errors::HTTPStatusError do
Expand Down

0 comments on commit 06766df

Please sign in to comment.