简体   繁体   中英

How to catch 404 and 500 error in Rails?

I am using parse-ruby-client in a Rails web app. According to the documentation at http://www.rubydoc.info/github/adelevie/parse-ruby-client/file/README.md#Logging_In , I can do

user = Parse::User.authenticate("cooldude6", "p_n7!-e8")

to log a user in. This works as long as the credentials are correct. If they're not correct, I get an error in the Rails app pointing to that line above.

In the logs, I see:

I, [2015-10-29T18:41:06.636916 #563]  INFO -- Status: 404
Completed 500 Internal Server Error in 625ms

My question is: how do I catch this 404 status and 500 internal server error so the page doesn't get rendered and throw this error? Ideally I'd want to redirect back to the sign in page if there is an error.

You need to rescue Parse::ParseProtocolError

user = Parse::User.authenticate(params[:username], params[:password])
  # continue normally
rescue Parse::ParseProtocolError => e
  status_code = e.code
  # handle error and try again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM