Category Archives: Errors

Create custom error pages dynamically in ruby on rails 4

The default status code templates are served by a Rack exception application. You can override this to be any Rack compatible app. To override this you need to add following line in your application.rb file.

# config/application.rb
config.exceptions_app = self.routes

Now we need to create ErrorsController:

class ErrorsController < ApplicationController
  def show
    @exception = env['action_dispatch.exception']
    write_to_log_file
    render action: request.path[1..-1]
  end

  def write_to_log_file
    if @exception.present?
      logger.error("Error occured: #{@exception.message}, Status: #{status_code}\n Time: #{Time.now}")
    end
  end

  protected

  def status_code
    params[:code] || 500
  end
end

In show method we have added a function to write the exception to log file.

Now create templates for each status code in errors directory. I have created three template for 404.html.erb, 422.html.erb and 500.html.erb

# app/views/errors/404.html.haml
%h1 404 - Not Found

Now create route for this errors:

# config/routes.rb
 %w( 404 422 500 ).each do |code|
    get code, :to => 'errors#show', :code => code
 end
 

And that’s it for dynamic error page.

Happy coding 🙂

Mysql ‘Got error -1 from storage engine’ error

Today I got an error while updating my mysql record . I got “Mysql ‘Got error -1 from storage engine’ error” every time I try to update.

Below are the logs I found from my mysql error logs:

MySQL Error log path: /var/log/mysql/error.log

InnoDB: A new raw disk partition was initialized or
InnoDB: innodb_force_recovery is on: we do not allow
InnoDB: database modifications by the user. Shut down
InnoDB: mysqld and edit my.cnf so that newraw is replaced
InnoDB: with raw, and innodb_force_... is removed.

Finally I got solution for that. To solve this issue follow below steps.

1. Stop your mysql server

 sudo service mysql stop 

2. Set innodb_force_recovery to 0 or remove this line from /etc/mysql/my.cnf

 innodb_force_recovery = 0 

3. Start your mysql server

 sudo service mysql start 

This will solve your problem.

Happy Coding 🙂

The program ‘bundle’ is currently not installed

This is a common problem when you first time install a new version of ruby. To fix this problem please follow below steps:

Step one:
Set up the PATH variable for the installed ruby executable
(Suppose the installed ruby is ruby-2.1.5):

Step two:

PATH=$PATH:$HOME/.rvm/gems/ruby-2.1.5/bin

Step three:

gem install bundler

(if bundler is not installed)

Final Step:

rvm use ruby-2.1.5 --default

(–default is optional).

This will fix your problem.

Hope it helps 🙂