1

I'm writing REST API for application with Hanami ruby web framework, and I'm wondering how to set error format to JSON. For example,when 404 error occurred it should return response with body {'message': 'Method not found'}, and Content-Type: 'application/json'

Eyeslandic
  • 14,553
  • 13
  • 41
  • 54

2 Answers2

1

You need to set format to json.

format: :json

See Mime Types Documentation

Antarr Byrd
  • 24,863
  • 33
  • 100
  • 188
0

Stumbled upon this while searching for something else, so thought I'd answer it even though it's a really old question.

One quick way to do this would be to add a catch-all route at the very end of your routes. e.g.

# apps/web/config/routes.rb

get '/*', to: ->(env) { [404, { 'Content-Type' => 'application/json' }, ["Method not found"]] }

Since you're using Hanami, the http-router configuration (where you could usually define a default_app option) isn't available to you directly.

See lib/hanami/routing/default.rb for the default "not found" action if you prefer to consider another approach with monkey-patching (or some way to supply a configuration option that I'm not aware of at this point).