about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2021-03-01 04:59:13 +0100
committerGitHub <noreply@github.com>2021-03-01 04:59:13 +0100
commit9aa37b32c3307dcb5896e1b768967666a6fdbf65 (patch)
treed10630dcc1a46d450be7b711e9ae47c91492d2cf /app
parentb4cb8c3c8356e226061fd70ae2139318c6a558e5 (diff)
Add `details` to error response for `POST /api/v1/accounts` in REST API (#15803)
Diffstat (limited to 'app')
-rw-r--r--app/controllers/api/v1/accounts_controller.rb2
-rw-r--r--app/lib/validation_error_formatter.rb32
-rw-r--r--app/validators/blacklisted_email_validator.rb4
-rw-r--r--app/validators/email_mx_validator.rb11
-rw-r--r--app/validators/note_length_validator.rb2
-rw-r--r--app/validators/unique_username_validator.rb2
-rw-r--r--app/validators/unreserved_username_validator.rb5
7 files changed, 48 insertions, 10 deletions
diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb
index 953874e1a..996f1b79b 100644
--- a/app/controllers/api/v1/accounts_controller.rb
+++ b/app/controllers/api/v1/accounts_controller.rb
@@ -27,6 +27,8 @@ class Api::V1::AccountsController < Api::BaseController
 
     self.response_body = Oj.dump(response.body)
     self.status        = response.status
+  rescue ActiveRecord::RecordInvalid => e
+    render json: ValidationErrorFormatter.new(e, :'account.username' => :username, :'invite_request.text' => :reason).as_json, status: :unprocessable_entity
   end
 
   def follow
diff --git a/app/lib/validation_error_formatter.rb b/app/lib/validation_error_formatter.rb
new file mode 100644
index 000000000..3f964f739
--- /dev/null
+++ b/app/lib/validation_error_formatter.rb
@@ -0,0 +1,32 @@
+# frozen_string_literal: true
+
+class ValidationErrorFormatter
+  def initialize(error, aliases = {})
+    @error   = error
+    @aliases = aliases
+  end
+
+  def as_json
+    { error: @error.to_s, details: details }
+  end
+
+  private
+
+  def details
+    h = {}
+
+    errors.details.each_pair do |attribute_name, attribute_errors|
+      messages = errors.messages[attribute_name]
+
+      h[@aliases[attribute_name] || attribute_name] = attribute_errors.map.with_index do |error, index|
+        { error: 'ERR_' + error[:error].to_s.upcase, description: messages[index] }
+      end
+    end
+
+    h
+  end
+
+  def errors
+    @errors ||= @error.record.errors
+  end
+end
diff --git a/app/validators/blacklisted_email_validator.rb b/app/validators/blacklisted_email_validator.rb
index 20a1587cc..1ca73fdcc 100644
--- a/app/validators/blacklisted_email_validator.rb
+++ b/app/validators/blacklisted_email_validator.rb
@@ -2,11 +2,11 @@
 
 class BlacklistedEmailValidator < ActiveModel::Validator
   def validate(user)
-    return if user.valid_invitation?
+    return if user.valid_invitation? || user.email.blank?
 
     @email = user.email
 
-    user.errors.add(:email, I18n.t('users.blocked_email_provider')) if blocked_email?
+    user.errors.add(:email, :blocked) if blocked_email?
   end
 
   private
diff --git a/app/validators/email_mx_validator.rb b/app/validators/email_mx_validator.rb
index ef1554494..9f70a1469 100644
--- a/app/validators/email_mx_validator.rb
+++ b/app/validators/email_mx_validator.rb
@@ -4,16 +4,19 @@ require 'resolv'
 
 class EmailMxValidator < ActiveModel::Validator
   def validate(user)
+    return if user.email.blank?
+
     domain = get_domain(user.email)
 
-    if domain.nil?
-      user.errors.add(:email, I18n.t('users.invalid_email'))
+    if domain.blank?
+      user.errors.add(:email, :invalid)
     else
       ips, hostnames = resolve_mx(domain)
+
       if ips.empty?
-        user.errors.add(:email, I18n.t('users.invalid_email_mx'))
+        user.errors.add(:email, :unreachable)
       elsif on_blacklist?(hostnames + ips)
-        user.errors.add(:email, I18n.t('users.blocked_email_provider'))
+        user.errors.add(:email, :blocked)
       end
     end
   end
diff --git a/app/validators/note_length_validator.rb b/app/validators/note_length_validator.rb
index 5ff6df6df..7ea2bb3e5 100644
--- a/app/validators/note_length_validator.rb
+++ b/app/validators/note_length_validator.rb
@@ -2,7 +2,7 @@
 
 class NoteLengthValidator < ActiveModel::EachValidator
   def validate_each(record, attribute, value)
-    record.errors.add(attribute, I18n.t('statuses.over_character_limit', max: options[:maximum])) if too_long?(value)
+    record.errors.add(attribute, :too_long, message: I18n.t('statuses.over_character_limit', max: options[:maximum]), count: options[:maximum]) if too_long?(value)
   end
 
   private
diff --git a/app/validators/unique_username_validator.rb b/app/validators/unique_username_validator.rb
index f87eb06ba..09c8fadb5 100644
--- a/app/validators/unique_username_validator.rb
+++ b/app/validators/unique_username_validator.rb
@@ -4,7 +4,7 @@
 
 class UniqueUsernameValidator < ActiveModel::Validator
   def validate(account)
-    return if account.username.nil?
+    return if account.username.blank?
 
     normalized_username = account.username.downcase
     normalized_domain = account.domain&.downcase
diff --git a/app/validators/unreserved_username_validator.rb b/app/validators/unreserved_username_validator.rb
index 634ceb06e..974f3ba62 100644
--- a/app/validators/unreserved_username_validator.rb
+++ b/app/validators/unreserved_username_validator.rb
@@ -3,9 +3,10 @@
 class UnreservedUsernameValidator < ActiveModel::Validator
   def validate(account)
     @username = account.username
-    return if @username.nil?
 
-    account.errors.add(:username, I18n.t('accounts.reserved_username')) if reserved_username?
+    return if @username.blank?
+
+    account.errors.add(:username, :reserved) if reserved_username?
   end
 
   private