Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add details to error response for POST /api/v1/accounts in REST API #15803

Merged
merged 1 commit into from Mar 1, 2021

Conversation

Gargron
Copy link
Member

@Gargron Gargron commented Feb 27, 2021

Example error response:

{
  "error": "Validation failed: Password can't be blank, Username must contain only letters, numbers and underscores, Agreement must be accepted",
  "details": {
    "password": [
      {
        "error": "ERR_BLANK",
        "description": "can't be blank"
      }
    ],
    "username": [
      {
        "error": "ERR_INVALID",
        "description": "must contain only letters, numbers and underscores"
      }
    ],
    "agreement": [
      {
        "error": "ERR_ACCEPTED",
        "description": "must be accepted"
      }
    ]
  }
}

Note: API responses always use en locale.

This PR requires some i18n changes because the way we've been handling error messages so far is not 100% compatible with being able to pass error names from validations.

Error
ERR_BLOCKED When e-mail provider is not allowed
ERR_UNREACHABLE When e-mail address does not resolve to any IP via DNS (MX, A, AAAA)
ERR_TAKEN When username or e-mail are already taken
ERR_RESERVED When a username is reserved, e.g. "webmaster" or "admin"
ERR_ACCEPTED When agreement has not been accepted
ERR_BLANK When a required attribute is blank
ERR_INVALID When an attribute is malformed, e.g. wrong characters or invalid e-mail address
ERR_TOO_LONG When an attribute is over the character limit
ERR_TOO_SHORT When an attribute is under the character requirement
ERR_INCLUSION When an attribute is not one of the allowed values, e.g. unsupported locale

@Gargron Gargron added the api REST API, Streaming API, Web Push API label Feb 27, 2021
@Gargron Gargron force-pushed the feature-api-validation-errors branch from d78f80f to 3a4f418 Compare February 28, 2021 06:03
@Gargron Gargron marked this pull request as ready for review February 28, 2021 06:05
@Gargron Gargron force-pushed the feature-api-validation-errors branch 2 times, most recently from 8ffa8c4 to 40a5eb0 Compare February 28, 2021 06:40
@Gargron Gargron force-pushed the feature-api-validation-errors branch from 40a5eb0 to f472e32 Compare February 28, 2021 07:04
@nightpool
Copy link
Member

Beyond the discussions about i18n we had in chat:

  • ERR_ACCEPTED and ERR_BLANK could maybe be combined into a more general ERR_REQUIRED? or we can replace ERR_ACCEPTED with ERR_BLANK

@@ -27,6 +27,8 @@ def create

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the aliases hash might be a little clearer here as a keyword argument, i.e.

render json: ValidationErrorFormatter.new(e, aliaes: {
    :'account.username' => :username,
    :'invite_request.text' => :reason
  }).as_json,
  status: :unprocessable_entity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api REST API, Streaming API, Web Push API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants