Skip to content

First pass at documenting error rendering #13

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

Closed
wants to merge 1 commit into from
Closed
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
50 changes: 48 additions & 2 deletions source/guides/serialization/errors.html.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,54 @@
---
layout: guides
---
# Serialization
# Serialization - Rendering errors

## Rendering errors
You can also use jsonapi-rb to render JSON API errors.

## Plain ruby

When using jsonapi-rb in plain ruby (or from within a framework outside of a
controller), you can render an errors document as follows:

```ruby
JSONAPI::Serializable::ErrorRenderer.render([
JSONAPI::Serializable::Error.create({
status: "422",
source: { pointer: "/data/attributes/first-name" },
title: "Invalid Attribute",
detail: "First name must contain at least three characters."
})
])
```

## Ruby on Rails

When using jsonapi-rb with Rails (via the jsonapi-rails gem), rendering errors is done
via the usual `render` controller method by using the `jsonapi_errors` renderer:

```ruby
render jsonapi_errors: {
status: "422",
source: { "pointer": "/data/attributes/volume" },
detail: "Volume does not, in fact, go to 11."
}
```

You can also pass `ActiveModel::Errors`

```ruby
render jsonapi_errors: post.errors
```

or you can pass an array of objects

```ruby
render jsonapi_errors: [post.errors, author.errors, {
status: "400",
detail: "JSON parse error - Expecting property name at line 1 column 2 (char 1)."
}]
```

## Hanami

Soon.