Tuesday, October 13, 2009

404 in Sinatra

‹prev | My Chain | next›

Before going live, we need legit 404 and 500 error messages. In production mode, the default Sinatra error messages are a little unhelpful:



Sinatra provides two code blocks for handling errors:
not_found do
end

error do
end
I like having my not found / error templates to be named 404 / 500. Since I am using Haml, I can use the 404.haml and 500.haml templates:
not_found do
haml :'404'
end

error do
haml :'500'
end
I use :'404' because the haml method requires a symbol and :404 is not a valid symbol. After defining the contents of the Haml templates, I have my 404 and 500 error pages.

The only other thing that I do tonight is define the kid nicknames document. We share a lot of personal information about our family in the cookbook, but never the actual kids' names (we're even a little vague about birthdays). Maybe we're paranoid, but...

I have a helper that pulls the nicknames from CouchDB:
    def kid_nicknames
@@kid_kicknames ||= JSON.parse(RestClient.get("#{_db}/kids"))
end
Those nicknames are then used as part of the "wiki" method:
    def wiki(original, convert_textile=true)
text = (original || '').dup
#...
text.gsub!(/\[kid:(\w+)\]/m) { |kid| kid_nicknames[$1] }
#...
end
The seed data for the kids kids document has been empty. I define it:
{
"___":"our daughter",
"___":"our son",
"___":"the baby"
}
And then use my couch_docs gem to load into the CouchDB database:
jaynestown% couch-docs load . http://localhost:5984/eee
With that, I have the nicknames displaying correctly:



That will do for tonight. Tomorrow, I deploy the 404/500 and seed data to beta. Then I can switch beta to be the production site, thus ending my chain.

1 comment: