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

Extend add_error to also support complex error arrays, hashes #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SpComb
Copy link

@SpComb SpComb commented May 17, 2017

We are using mutations in the Kontena Server, and we have two usecases related to validation errors that the current Mutations::Command API does not really support:

  • Reporting manual validation errors for multi-valued array fields, such as when doing a DB lookup for each array item in the validate method

    Our current approach for validating such fields looks something like this: GridServices::Common#validate_secrets_exist

          optional do
            array :secrets do
            hash do
              required do
                string :secret
                string :name
              end
            end
          end
    def validate_secrets_exist(grid, secrets)
      secrets.each do |s|
        unless grid.grid_secrets.find_by(name: s[:secret])
          add_error(:secrets, :not_found, "Secret #{s[:secret]} does not exist")
        end
      end
    end

    However, if there are multiple non-existant secrets in the secrets array, then we can only report one of those validation errors.

    Being able to report all of the resulting validation errors for each of the array items requires something to add a Mutations::ErrorArray to @errors, which is not supported by the current API.

  • Nested mutations, where the Stacks::Create mutation takes an array of service hashes, and passes each of those to the Service::Create mutation

    Our current approach of handling such nested outcome errors involves calling add_error for each field in the resulting outcome errors hash: Stacks::Common#handle_service_outcome_errors

    def handle_service_outcome_errors(service_name, errors)
      errors.each do |key, atom|
         add_error("services.#{service_name}.#{key}", atom.symbolic, atom.message)
      end
    end
    
    def validate
      self.services.each do |service|
        outcome = GridServices::Create.validate(service)
        unless outcome.success?
          handle_service_outcome_errors(service[:name], outcome.errors)
        end
      end
    end

    However, while this works okay for simple sub-mutation errors, this fails badly on any validation errors for any array fields, because there is no way to add the resulting Mutations::ErrorArray error to the top-level mutation @errors. Currently this fails with a ArgumentError (Invalid kind) when passing the array of symbols returned by Mutations::ErrorArray#symbolic to add_error: Syntax errors in kontena.yml service environment cause API 500 errors kontena/kontena#2238 (comment)

    The merge_errors method doesn't work either, because each of the identically-shaped sub-mutation outcome errors needs to be added as nested errors with some unique services.foo key.

This PR extends the Mutations::Command#add_error to provide a solution for both of these uses-cases by also accepting structured Mutations::ErrorAtom, Mutations::ErrorArray and Mutations::ErrorHash errors with a structured key. The simple case of a add_error(key, :kind, "message") is implicitly converted to a Mutations::ErrorAtom.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant