title: render JSON formatted response when accessing undefined routes in Rails API service
body: <p>Here is the situation:</p>
  
  <p>If a user try to access my rails-api service with an undefined route, rails would catch an exception and render an html with <code>Routing Error:</code> to the browser. </p>
  
  <p>But what i want my rails-api service to perform is that it can catch any errors like this and return a <code>json-formatted</code> error info to the request source, like below:</p>
  
  <pre><code># if i visit a route which is undefined in my config/routes.rb like this:
  
  HTTP.get('http://localhost:3000/api/v1/route/of/undefined')
  
  # what i want it to render to the page is:
  
  {status: 404, err_msg: 'route of undefined, please check again.'}
  </code></pre>
  
  <p>Before i take my action, i find that Rails matches a request with a route happens before it initializes a controller, thus if i add <code>rescue_from</code> to my <code>ApplicationController</code>,  ApplicationController has no chance to rescue the exception.</p>
  
  <p>also, i added line below to my project:</p>
  
  <pre><code># config/application.rb
  config.exceptions_app = self.routes
  
  # config/environments/development.rb
  config.consider_all_requests_local = false
  </code></pre>
  
  <p>what next should I do? i searched a lot but could not find an answer to solve this problem. especially in an <code>rails api service</code>.</p>