views:

80

answers:

3

Hi, I have a problem in displaying the error message in Ruby on Rails. I' am using

rescue => Exception ex
 #display ex.message

the output I get when I tried to display it in an alert messagebox is this:

"DBI::DatabaseError: 37000 (50000) [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot approve records for the specified date..: Exec uspTestProc 279, 167, 2."

it displays some words that are not friendly to users. what I want is, is to display only these words: "Cannot approve records for the specified date"

hope I made my self clear.

thanks guys for helping me out! Godbless!

A: 

In any language, I would usually always handle the exceptions and show the user a dumbed down version.

Users shouldn't get to see the inner workings of something and exceptions are a great way to show them a big mess of nonsense.

I:

  • Log the actual exception because I, or the system maintainer needs to know exactly what happened, with a tracelog if possible.
  • Show the user either a tailored exception for specific to the problem - "You've entered the wrong data!"
  • Or a generic error - "Oh noes! something went hideously wrong!!1" - if it wasn't caused by the user or I haven't got a case for handling it (yet).
Oli
A: 

Common practice in Rails is to use the "flash" session variable within the Controller:

# error catching logic goes here
flash[:error] = "There was an error!"

# notice logic goes here
flash[:notice] = "I am sending you a notice."

Then display it (possibly within a catchall layout):

<% if flash[:error] %>
<div id="error"><%= flash[:error] %></div>
<% end %>

<% if flash[:notice] %>
<div id="notice"><%= flash[:notice] %></div>
<% end %>

Was that what you are looking for?

bojo
Hi. thanks for the reply, the error was raised in ms sql using raiserror..ex.raiserror("Cannot approve records for the specified date")i like to display the word "Cannot approve records for the specified date"..but when i display it in ruby on rails what i got is this "DBI::DatabaseError: 37000 (50000) [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot approve records for the specified date..: Exec uspTestProc 279, 167, 2"hope you can help me with this..many thanks.
Ryan Jtt
+1  A: 

I think an error like that can be catch by rescue_from


class ApplicationController

  rescue_from MyException do
    render :text => 'We have some issue in our database'
  end
end
shingara
Hi. thanks for the reply, the error was raised in ms sql using raiserror..ex.raiserror("Cannot approve records for the specified date")i like to display the word "Cannot approve records for the specified date"..but when i display it in ruby on rails what i got is this "DBI::DatabaseError: 37000 (50000) [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot approve records for the specified date..: Exec uspTestProc 279, 167, 2"hope you can help me with this..many thanks.
Ryan Jtt