title: How to catch a 500 Internal Server Error in Rails
body: <p>I have done this tons of times before when fetching things from a database, etc. </p>
  
  <p>For my specific case I am using <a href="https://github.com/vigetlabs/ruby_spark/blob/master/lib/ruby_spark/device.rb" rel="nofollow noreferrer">a 3rd party</a> to connect to a piece of hardware... Anyways, in the case of an error, such as an <strong>invalid id</strong> obviously, we want to raise a exception or a rescue... but unfortunately I don't know how to raise it because by the time it is hit, it's too late (I think)</p>
  
  <p>Here...</p>
  
  <pre><code># 
  # getting params and saving item above...
  # 
  
  if item.save
    device = RubySpark::Device.new("FAKEUNITID800")
    device.function("req", "ITEM")
    redirect_to controller: 'items', action: 'edit_items'
  end
  </code></pre>
  
  <p>If this was a valid ID, everything would work, and it would take you to the <code>/edit</code> page! But the issue is, with an invalid ID, it just does...</p>
  
  <pre><code>Completed 500 Internal Server Error in 897ms
  
  RubySpark::Device::ApiError - Permission Denied: Invalid Device ID:
  </code></pre>
  
  <p>I checked out the following tutorials </p>
  
  <ul>
  <li><a href="https://robots.thoughtbot.com/rescue-standarderror-not-exception" rel="nofollow noreferrer">Rescue StandardError, Not Exception</a></li>
  <li><a href="https://stackoverflow.com/questions/33427586/how-to-catch-404-and-500-error-in-rails">How to catch 404 and 500 error in Rails?</a></li>
  <li><a href="https://mattbrictson.com/dynamic-rails-error-pages" rel="nofollow noreferrer">Dynamic Rails Error Pages</a></li>
  </ul>
  
  <p>But honestly, they just make me more confused. Maybe I have the wrong approach to this. I always thought that first you make the request, and then you have a fall back case, depending what status (ie. 200, 500, 404) you get... you go from there.</p>