From 26006f12a23352f9ec737b4c66318c3a2b4678fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 1 Dec 2015 15:38:21 +0100 Subject: [PATCH] Increased 'error resistance' in base importer class --- script/import_scripts/base.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/script/import_scripts/base.rb b/script/import_scripts/base.rb index ea98e73cb02..1632757cac4 100644 --- a/script/import_scripts/base.rb +++ b/script/import_scripts/base.rb @@ -239,14 +239,18 @@ class ImportScripts::Base elsif u[:email].present? new_user = create_user(u, import_id) - if new_user.valid? && new_user.user_profile.valid? + if new_user && new_user.valid? && new_user.user_profile && new_user.user_profile.valid? @lookup.add_user(import_id.to_s, new_user) created += 1 else failed += 1 - puts "Failed to create user id: #{import_id}, username: #{new_user.username}, email: #{new_user.email}" - puts "user errors: #{new_user.errors.full_messages}" - puts "user_profile errors: #{new_user.user_profile.errors.full_messages}" if new_user.user_profile.present? && new_user.user_profile.errors.present? + puts "Failed to create user id: #{import_id}, username: #{new_user.try(:username)}, email: #{new_user.try(:email)}" + if new_user.try(:errors) + puts "user errors: #{new_user.errors.full_messages}" + if new_user.try(:user_profile).try(:errors) + puts "user_profile errors: #{new_user.user_profile.errors.full_messages}" + end + end end else failed += 1 @@ -292,6 +296,7 @@ class ImportScripts::Base opts[:last_emailed_at] = opts.fetch(:last_emailed_at, Time.now) u = User.new(opts) + (opts[:custom_fields] || {}).each { |k, v| u.custom_fields[k] = v } u.custom_fields["import_id"] = import_id u.custom_fields["import_username"] = opts[:username] if opts[:username].present? u.custom_fields["import_avatar_url"] = avatar_url if avatar_url.present? @@ -313,9 +318,8 @@ class ImportScripts::Base end rescue => e # try based on email - if e.record.errors.messages[:email].present? - existing = User.find_by(email: opts[:email].downcase) - if existing + if e.try(:record).try(:errors).try(:messages).try(:[], :email).present? + if existing = User.find_by(email: opts[:email].downcase) existing.custom_fields["import_id"] = import_id existing.save! u = existing @@ -325,6 +329,7 @@ class ImportScripts::Base raise e end end + post_create_action.try(:call, u) if u.persisted? u # If there was an error creating the user, u.errors has the messages