mirror of
https://github.com/discourse/discourse.git
synced 2025-06-05 14:07:30 +08:00
Increased 'error resistance' in base importer class
This commit is contained in:
@ -239,14 +239,18 @@ class ImportScripts::Base
|
|||||||
elsif u[:email].present?
|
elsif u[:email].present?
|
||||||
new_user = create_user(u, import_id)
|
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)
|
@lookup.add_user(import_id.to_s, new_user)
|
||||||
created += 1
|
created += 1
|
||||||
else
|
else
|
||||||
failed += 1
|
failed += 1
|
||||||
puts "Failed to create user id: #{import_id}, username: #{new_user.username}, email: #{new_user.email}"
|
puts "Failed to create user id: #{import_id}, username: #{new_user.try(:username)}, email: #{new_user.try(:email)}"
|
||||||
puts "user errors: #{new_user.errors.full_messages}"
|
if new_user.try(:errors)
|
||||||
puts "user_profile errors: #{new_user.user_profile.errors.full_messages}" if new_user.user_profile.present? && new_user.user_profile.errors.present?
|
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
|
end
|
||||||
else
|
else
|
||||||
failed += 1
|
failed += 1
|
||||||
@ -292,6 +296,7 @@ class ImportScripts::Base
|
|||||||
opts[:last_emailed_at] = opts.fetch(:last_emailed_at, Time.now)
|
opts[:last_emailed_at] = opts.fetch(:last_emailed_at, Time.now)
|
||||||
|
|
||||||
u = User.new(opts)
|
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_id"] = import_id
|
||||||
u.custom_fields["import_username"] = opts[:username] if opts[:username].present?
|
u.custom_fields["import_username"] = opts[:username] if opts[:username].present?
|
||||||
u.custom_fields["import_avatar_url"] = avatar_url if avatar_url.present?
|
u.custom_fields["import_avatar_url"] = avatar_url if avatar_url.present?
|
||||||
@ -313,9 +318,8 @@ class ImportScripts::Base
|
|||||||
end
|
end
|
||||||
rescue => e
|
rescue => e
|
||||||
# try based on email
|
# try based on email
|
||||||
if e.record.errors.messages[:email].present?
|
if e.try(:record).try(:errors).try(:messages).try(:[], :email).present?
|
||||||
existing = User.find_by(email: opts[:email].downcase)
|
if existing = User.find_by(email: opts[:email].downcase)
|
||||||
if existing
|
|
||||||
existing.custom_fields["import_id"] = import_id
|
existing.custom_fields["import_id"] = import_id
|
||||||
existing.save!
|
existing.save!
|
||||||
u = existing
|
u = existing
|
||||||
@ -325,6 +329,7 @@ class ImportScripts::Base
|
|||||||
raise e
|
raise e
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
post_create_action.try(:call, u) if u.persisted?
|
post_create_action.try(:call, u) if u.persisted?
|
||||||
|
|
||||||
u # If there was an error creating the user, u.errors has the messages
|
u # If there was an error creating the user, u.errors has the messages
|
||||||
|
Reference in New Issue
Block a user