Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a to_json method for Granite::Error #401

Merged
merged 1 commit into from May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions spec/granite/error/error_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "../../spec_helper"

describe Granite::Error do
it "should convert to json" do
Granite::Error.new("field", "error message").to_json.should eq %({"field":"field","message":"error message"})
end
end
7 changes: 7 additions & 0 deletions src/granite/error.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ class Granite::Error
def initialize(@field : (String | Symbol | JSON::Any), @message : String? = "")
end

def to_json(builder : JSON::Builder)
builder.object do
builder.field "field", @field
builder.field "message", @message
end
end

def to_s(io)
if field == :base
io << message
Expand Down