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

Add a to_json method for Granite::Error #401

merged 1 commit into from May 12, 2020

Conversation

ghost
Copy link

@ghost ghost commented May 11, 2020

I wasn't sure if this was something that was at all wanted, so I figured I'd send a PR to be safe.

This PR manually defines a to_json method for Granite::Error. We can't include JSON::Serializable due to the type of Granite::Error.field, so a manual solution is required.

@Blacksmoke16
Copy link
Contributor

What is this actually needed for?

@ghost
Copy link
Author

ghost commented May 11, 2020

I found it useful for quickly and easily returning validation errors.

post = Post.new
post.save
if post.errors.size > 0
  post.errors.to_json
end

Like I said, wasn't sure if this was wanted or not. I use it in my codebase so I thought I'd send a PR.

Copy link
Member

@drujensen drujensen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 Can you add a small test to verify?

src/granite/error.cr Outdated Show resolved Hide resolved
src/granite/error.cr Outdated Show resolved Hide resolved
@ghost
Copy link
Author

ghost commented May 11, 2020

@drujensen for some reason I can't run tests on my local machine. Still, I'm not sure how I would test it. Just write a test that makes sure the json is valid?

@drujensen
Copy link
Member

drujensen commented May 11, 2020

@drujensen for some reason I can't run tests on my local machine. Still, I'm not sure how I would test it. Just write a test that makes sure the json is valid?

Correct. You can test just the Error class. Something like (pseudo code):

error = Granite::Error.new "field", "error message"
expect error.to_json to equal {field: "error message"}

@drujensen drujensen requested a review from Blacksmoke16 May 12, 2020 12:07
@drujensen drujensen merged commit af36d1e into amberframework:master May 12, 2020
@ghost
Copy link
Author

ghost commented May 12, 2020

thanks!

@ghost ghost deleted the json-errors branch May 12, 2020 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants