Skip to content

Instantly share code, notes, and snippets.

@kdiogenes
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save kdiogenes/e9d76080feeb8f5bae9d to your computer and use it in GitHub Desktop.

Select an option

Save kdiogenes/e9d76080feeb8f5bae9d to your computer and use it in GitHub Desktop.
Ruby: Exception handling
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
@kdiogenes
Copy link
Copy Markdown
Author

I would like to be able to write this as:

rescue DiscourseApi::Error => e
  discourse_errors = JSON.parse(e.message.gsub('=>',':'))['errors']
  discourse_errors.each { |error| errors.add(:field, error) }
  false
rescue
  unknow_error
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment