Last active
August 29, 2015 14:21
-
-
Save kdiogenes/e9d76080feeb8f5bae9d to your computer and use it in GitHub Desktop.
Ruby: Exception handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def save | |
| tour = Tour.find(tour_id) | |
| discourse_user = DiscourseZr.client.user_by_external_id(user_id) | |
| discourse_user = DiscourseZr.sync_sso(User.find(user_id)) if discourse_user.nil? | |
| client = DiscourseZr.client(username: discourse_user['username']) | |
| client.create_post(topic_id: tour.discourse_topic_id, raw: content) | |
| rescue DiscourseApi::Error => e | |
| begin | |
| discourse_errors = JSON.parse(e.message.gsub('=>',':'))['errors'] | |
| discourse_errors.each { |error| errors.add(:field, error) } | |
| false | |
| rescue | |
| unknow_error | |
| end | |
| rescue | |
| unknow_error | |
| end | |
| private | |
| def unknow_error | |
| errors.add(:base, 'Ocorreu um erro desconhecido, por favor tente novamente. Se o problema persistir, contate o administrador do sistema.') | |
| false | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would like to be able to write this as: