Ability to change status code from turbine:on_exception


Hi, I’m wondering if there is a way to manipulate the response from the on_exception hook for a turbine.

My particular use case right now is, gets from the database result in a 502 back to the browser if the record doesn’t exist…eg

GET /users/1000 throws an ActiveRecord:RecordNotFound which ultimately ends up returning a 502 to the client.

I’d like to capture all the RecordNotFound exceptions and change status code to 404. Is it possible to do this with a Turbine or do I have to do something with inheritance etc?


Thinking would probably handle it in the controller instead of a Turbine. Maybe something like this:

class PostsController < ApplicationController
  # before_action :set_post, only: [:show, :edit, :update, :delete]

  # GET /posts/1
  def show
    @post = Post.find(params[:id]) # Note: the before_action filter has been removed
  rescue ActiveRecord::RecordNotFound
    render file: "#{Jets.root}/public/404.html", status: 404

Log output:

I, [2019-04-30T01:06:46.543175 #5860]  INFO -- : Started GET "/posts/8" for at 2019-04-30 01:06:46 +0000
I, [2019-04-30T01:06:46.543227 #5860]  INFO -- : Processing PostsController#show
I, [2019-04-30T01:06:46.543293 #5860]  INFO -- :   Event: {"resource":"/posts/{id}","path":"/posts/8","httpMethod":"GET","headers":{"Host":"localhost:8888","User-Agent":"curl/7.53.1","Accept":"*/*","Version":"HTTP/1.1"},"queryStringParameters":{},"pathParameters":{"id":"8"},"stageVariables":null,"requestContext":{},"body":null,"isBase64Encoded":false}
I, [2019-04-30T01:06:46.543350 #5860]  INFO -- :   Parameters: {"id":"8"}
I, [2019-04-30T01:06:46.594699 #5860]  INFO -- : Completed Status Code 404 in 0.051555544s - - [30/Apr/2019:01:06:46 +0000] "GET /posts/8 HTTP/1.1" 404 - 0.0665


Thanks for the tip…I’ve thought of that but was hoping to find something where I didn’t have to replicate the the rescue in every controller. I’d be doing this in show, update and possibly delete ( debatable ) across all controllers.
I’ve also considered inheritance or some implementation through a parent but that still doesn’t seem as clean as the turbine.


Oh I see. Just realized this part of the question

capture all the RecordNotFound exceptions

Um. cant think of anything at the moment. :thinking: Would be nice to be able to do something like this


@balutbomber Here you go http://rubyonjets.com/docs/extras/rescue-from/ Courtesy of https://github.com/tongueroo/jets/pull/211 :grin:


class ApplicationController < Jets::Controller::Base
  rescue_from ActiveRecord::RecordNotFound do |exception|
    render json: { message: "We could not find your post." }, status: 404

  def missing_post
    render json: { message: "We could not find your post." }, status: 404


AWESOME!!! Sorry I missed that in the documentation…been a little bit since I’ve been on…maybe it would be a good exercise for me to reread the docs . DOH :flushed: