diff --git a/ee/app/graphql/mutations/ai/self_hosted_models/create.rb b/ee/app/graphql/mutations/ai/self_hosted_models/create.rb index 6ce2f049b2b33848977e51f940d3e13a55194d41..57599ac73955e7431af25198c2ea32aa660d8bd5 100644 --- a/ee/app/graphql/mutations/ai/self_hosted_models/create.rb +++ b/ee/app/graphql/mutations/ai/self_hosted_models/create.rb @@ -35,7 +35,12 @@ def resolve(**args) { self_hosted_model: model, - errors: errors_on_object(model) + errors: [] # Errors are rescued below + } + rescue ActiveRecord::RecordInvalid => e + { + self_hosted_model: nil, + errors: [e.message] } end end diff --git a/ee/spec/requests/api/graphql/ai/self_hosted_models/create_spec.rb b/ee/spec/requests/api/graphql/ai/self_hosted_models/create_spec.rb index c24fc49077bce408dc9f816482615c6727097f03..5038eb96de117be49c17678eadb6a323ce9176b6 100644 --- a/ee/spec/requests/api/graphql/ai/self_hosted_models/create_spec.rb +++ b/ee/spec/requests/api/graphql/ai/self_hosted_models/create_spec.rb @@ -48,22 +48,46 @@ end context 'when user is allowed to write changes' do - let(:expected_result) do - { - "name" => 'ollama1-mistral', - "endpoint" => 'http://localhost:8080', - "model" => 'mistral', - "hasApiToken" => true - } - end - it_behaves_like 'it calls the manage_ai_settings policy' - it 'creates a self-hosted model' do - post_graphql_mutation(mutation, current_user: current_user) + context 'when there are errors with creating the self-hosted model' do + let(:input) do + { + "name" => '', + "endpoint" => 'http://localhost:8080', + "model" => 'MISTRAL', + "api_token" => "test_api_token" + } + end + + it 'returns an error message' do + post_graphql_mutation(mutation, current_user: current_user) + + expect(mutation_response['selfHostedModel']).to be(nil) + expect(mutation_response['errors']).to include("Validation failed: Name can't be blank") + end + + it 'does not create a self-hosted model' do + expect { request }.not_to change { ::Ai::SelfHostedModel.count } + end + end - expect(response).to have_gitlab_http_status(:success) - expect(mutation_response['selfHostedModel']).to include(**expected_result) + context 'when there are no errors' do + let(:expected_result) do + { + "name" => 'ollama1-mistral', + "endpoint" => 'http://localhost:8080', + "model" => 'mistral', + "hasApiToken" => true + } + end + + it 'creates a self-hosted model' do + post_graphql_mutation(mutation, current_user: current_user) + + expect(response).to have_gitlab_http_status(:success) + expect(mutation_response['selfHostedModel']).to include(**expected_result) + end end end end