Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8344425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:22:04+00:00 2026-06-09T06:22:04+00:00

I have a software as a service app that uses JSON communicated via a

  • 0

I have a “software as a service” app that uses JSON communicated via a RESTful API.

Simply stated: what are the best practices for capturing and reporting exceptions when using a RESTful API with JSON data interchange?

My first thought was to see what Rails does by generating a scaffold, but that’s clearly not right. Here’s an excerpt:

class MumblesController < ApplicationController

  # GET /mumbles/1
  # GET /mumbles/1.json
  def show
    @mumble = Mumble.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @mumble }
    end
  end

end

In this case, if the JSON code sends a non-existent ID, e.g.

http://www.myhost.com/mumbles/99999.json

then Mumble.find() will raise ActiveRecord::RecordNotFound. ActionController will catch that and render an error page in HTML. But HTML is useless to the client that is expecting JSON.

I could work around that by wrapping the Mumble.find() in a begin ... rescue RuntimeError block and rendering a JSON status => :unprocessable_entity or something.

But then what if the client’s app sends an invalid path, e.g.:

http://www.myhost.com/badtypo/1.json

Is a JSON based app supposed to catch that and return an error in JSON? If so, where do I capture that without digging deep into ActionDispatch?

So overall, do I punt and let ActionController generate HTML if there’s an error? That doesn’t feel right…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:22:07+00:00Added an answer on June 9, 2026 at 6:22 am

    (I found the answer just before I hit [Post your question]. But this might help someone else as well…)

    Use ActionController’s rescue_from

    The answer is to use ActionController’s rescue_from, as described in this Guide and documented here. In particular, you can replace the default rendering of the default 404.html and 500.html files along these lines:

    class ApplicationController < ActionController::Base
      rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
    
    private
      def record_not_found(error)
        render :json => {:error => error.message}, :status => :not_found
      end 
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that is installed per-machine (since it uses a service). One
I have a GWT application that I want to release as software-as-a-service (SAAS) on
I have a windows installer project which installs some software (winform, service, mce addin).
We have software that runs fine under a normal Windows installation but when we
I have a software that is tracking an NTFS volume changes on a Windows
We have this software that has a webservices component. Now, the administrator of this
I have a software that runs on mac (using terminal program) the program is
I have a software renderer that I am porting from PC to the iPhone.
I have some software written in VB.NET that performs a lot of calculations, mostly
I have a particular piece of software that i wish to load on windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.