Extend add_error to also support complex error arrays, hashes #114
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We are using
mutationsin the Kontena Server, and we have two usecases related to validation errors that the currentMutations::CommandAPI does not really support:Reporting manual validation errors for multi-valued
arrayfields, such as when doing a DB lookup for each array item in thevalidatemethodOur current approach for validating such fields looks something like this:
GridServices::Common#validate_secrets_existHowever, if there are multiple non-existant secrets in the
secretsarray, 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::ErrorArrayto@errors, which is not supported by the current API.Nested mutations, where the
Stacks::Createmutation takes an array ofservicehashes, and passes each of those to theService::CreatemutationOur current approach of handling such nested outcome errors involves calling
add_errorfor each field in the resulting outcome errors hash:Stacks::Common#handle_service_outcome_errorsHowever, while this works okay for simple sub-mutation errors, this fails badly on any validation errors for any
arrayfields, because there is no way to add the resultingMutations::ErrorArrayerror to the top-level mutation@errors. Currently this fails with aArgumentError (Invalid kind)when passing the array of symbols returned byMutations::ErrorArray#symbolictoadd_error: Syntax errors in kontena.yml service environment cause API 500 errors kontena/kontena#2238 (comment)The
merge_errorsmethod doesn't work either, because each of the identically-shaped sub-mutation outcome errors needs to be added as nested errors with some uniqueservices.fookey.This PR extends the
Mutations::Command#add_errorto provide a solution for both of these uses-cases by also accepting structuredMutations::ErrorAtom,Mutations::ErrorArrayandMutations::ErrorHasherrors with a structured key. The simple case of aadd_error(key, :kind, "message")is implicitly converted to aMutations::ErrorAtom.