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

322 Views Asked by At

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
1

There are 1 best solutions below

1
Maciej Pankanin On

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)