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
mutations
in the Kontena Server, and we have two usecases related to validation errors that the currentMutations::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 thevalidate
methodOur current approach for validating such fields looks something like this:
GridServices::Common#validate_secrets_exist
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 ofservice
hashes, and passes each of those to theService::Create
mutationOur 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
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 resultingMutations::ErrorArray
error to the top-level mutation@errors
. Currently this fails with aArgumentError (Invalid kind)
when passing the array of symbols returned byMutations::ErrorArray#symbolic
toadd_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 uniqueservices.foo
key.This PR extends the
Mutations::Command#add_error
to provide a solution for both of these uses-cases by also accepting structuredMutations::ErrorAtom
,Mutations::ErrorArray
andMutations::ErrorHash
errors with a structured key. The simple case of aadd_error(key, :kind, "message")
is implicitly converted to aMutations::ErrorAtom
.