Ayush Poddar
Ayush Poddar

Reputation: 106

Rails [Grape::API] How to include meta object when using error! method?

I am using Grape::API gem to build APIs and responses.

I figured a way to add the meta object to the response body when responding using present method. It is something like this:

present meta: { key: "value" }
present user, with: UserEntity

However, when I want to add the meta object with an error using this:

present meta: { key: "value" }
error!("Error message", 422)

I do not get the meta object in the response body. How do I add the meta object?


P.S: Currently I have defined a hack-ish method:

def present_error(message, status_code, meta: nil)
  body = {}
  body[:errors] = [{ title: message }]
  body[:meta] = meta if meta

  status status_code
  present body
end

Upvotes: 0

Views: 386

Answers (1)

Maciej Pankanin
Maciej Pankanin

Reputation: 48

I don't think there is nice solution like the one with present here.

Probably best you can do is:

error!({ error: "Error message", meta: meta }, 422)

Upvotes: 1

Related Questions