I have a restful api built on top of a rails 3 app, i am raising a custom exception error whenever an entity is not found like below
@line = @current_account.lines.find(:first, :conditions =>{:title => line_title})
raise CustomExceptionError.new("Line not found for title: #{line_title} under your account",404) if @line.nil?
here is the look at the CustomExceptionError class
class CustomExceptionError < StandardError
attr_accessor :message, :status
def initialize(message,status=422)
@message = message
@status = status
end
def to_s
"#{@message}"
end
end
and here is the method that is used to handle the exception
def custom_error_renderer(exception = nil)
if exception
logger.info "Rend开发者_开发技巧ering 404: #{exception.message}"
end
error = {:error => {:message => exception ? exception.message : "Undefined error."}}
render :json => error, :status => exception.status
end
But when i look at the server log
18:12:09 action_controller Completed in 18ms
18:12:09 action_controller Rendering 404: Line not found for title: dater98 under your account
the status code is missing in the log
if i render directly instead of raising the CustomExceptionError
render :status => 404, :json => {:error => {:message=> "Line not found for title: #{line_title} under your account""}}
then it returns the status code.
Completed 404 Not Found in 84ms (Views: 75.7ms)
I have no idea what i am doing wrong here. any suggestions or guess???
If you render with a status, it will send the standard 404 response from your public/404.html file.
You could try deleting your public/404.html file.
精彩评论