How to display descriptive error message?
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]Canno开发者_JAVA百科t 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 to display only these words: "Cannot approve records for the specified date"
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?
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
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).
精彩评论