Skip to content

Commit

Permalink
Set errors instead of exceptions in WorkflowFiltersValidator
Browse files Browse the repository at this point in the history
Set validation errors, so the user can know all the non-valid
attributes.

Also improve error messages and adapt specs.
  • Loading branch information
saraycp committed Sep 10, 2021
1 parent 4f62b2e commit bee6740
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
11 changes: 0 additions & 11 deletions src/api/app/models/workflow/errors.rb

This file was deleted.

10 changes: 7 additions & 3 deletions src/api/app/validators/workflow_filters_validator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class WorkflowFiltersValidator < ActiveModel::Validator
def validate(record)
@workflow = record
@workflow_instructions = record.workflow_instructions

valid_filters?
Expand All @@ -11,12 +12,15 @@ def valid_filters?
# Filters aren't mandatory in a workflow
return unless @workflow_instructions.key?(:filters)

raise Workflow::Errors::UnsupportedWorkflowFilters, "Unsupported filters: #{unsupported_filters.keys.to_sentence}" if unsupported_filters.present?
if unsupported_filters.present?
@workflow.errors.add(:base,
"Unsupported filters: #{unsupported_filters.keys.to_sentence}")
end

return unless unsupported_filter_types?

raise Workflow::Errors::UnsupportedWorkflowFilterTypes,
"Filters #{unsupported_filter_types.to_sentence} have unsupported keys. #{Workflow::SUPPORTED_FILTER_TYPES.to_sentence} are the only supported keys."
@workflow.errors.add(:base,
"Filters #{unsupported_filter_types.to_sentence} have unsupported keys, #{Workflow::SUPPORTED_FILTER_TYPES.to_sentence} are the only supported keys")
end

def unsupported_filters
Expand Down
33 changes: 27 additions & 6 deletions src/api/spec/models/workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@
}
end

it 'raises a user-friendly error message' do
expect do
subject.valid?
end.to raise_error(Workflow::Errors::UnsupportedWorkflowFilterTypes, "Filters #{filter} have unsupported keys. only and ignore are the only supported keys.")
it 'sets validation errors' do
expect(subject.errors.full_messages).to match_array(
["Filters #{filter} have unsupported keys, only and ignore are the only supported keys"]
)
end
end
end
Expand All @@ -209,8 +209,29 @@
}
end

it 'raises a user-friendly error message' do
expect { subject.valid? }.to raise_error(Workflow::Errors::UnsupportedWorkflowFilters, 'Unsupported filters: unsupported_1 and unsupported_2')
it 'sets validation errors' do
expect(subject.errors.full_messages).to match_array(
['Unsupported filters: unsupported_1 and unsupported_2']
)
end
end

context 'with a combination of unsupported filters and non-valid types' do
let(:yaml) do
{
'filters' => {
'unsupported_1' => { 'only' => ['foo'] },
'repositories' => { 'onlyyy' => [{ 'non_valid' => ['ppc'] }, 'x86_64'], 'ignore' => ['i586'] }
},
'steps' => [{ 'branch_package' => { source_project: 'project',
source_package: 'package' } }]
}
end

it 'sets validation errors' do
expect(subject.errors.full_messages).to match_array(
['Filters repositories have unsupported keys, only and ignore are the only supported keys', 'Unsupported filters: unsupported_1']
)
end
end
end
Expand Down

0 comments on commit bee6740

Please sign in to comment.