Skip to content

Commit

Permalink
chore(update_channel): better errors
Browse files Browse the repository at this point in the history
using the upstream branch of `ash_graphql` to handle errors in the
`update_channel` resource, this commit should be squashed to the
previous one when `ash_graphql` releases a new version.

Signed-off-by: Luca Zaninotto <[email protected]>
  • Loading branch information
lusergit committed Jan 7, 2025
1 parent a495750 commit c82adb8
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#
# This file is part of Edgehog.
#
# Copyright 2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.UpdateCampaigns.UpdateChannel.ErrorHandler do
@moduledoc false

def handle_error(error, context) do
%{action: action} = context

case action do
:create -> target_ids_translation(error)
:update -> target_ids_translation(error)
_ -> error
end
end

defp target_ids_translation(error) do
case error[:code] do
"not_found" ->
%{
error
| fields: [:target_group_ids],
message: "One or more target groups could not be found"
}

_ ->
error
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule Edgehog.UpdateCampaigns.UpdateChannel do

alias Edgehog.UpdateCampaigns.UpdateChannel.Calculations
alias Edgehog.UpdateCampaigns.UpdateChannel.Changes
alias Edgehog.UpdateCampaigns.UpdateChannel.ErrorHandler

resource do
description """
Expand All @@ -40,6 +41,8 @@ defmodule Edgehog.UpdateCampaigns.UpdateChannel do

graphql do
type :update_channel

error_handler {ErrorHandler, :handle_error, []}
end

actions do
Expand Down
2 changes: 1 addition & 1 deletion backend/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ defmodule Edgehog.MixProject do
{:credo, "~> 1.6", only: [:dev, :test], runtime: false},
{:ash, "~> 3.0"},
{:ash_postgres, "~> 2.0"},
{:ash_graphql, "~> 1.0"},
{:ash_graphql, github: "ash-project/ash_graphql"},
{:ash_json_api, "~> 1.3"},
{:picosat_elixir, "~> 0.2"},
{:styler, "~> 1.0.0-rc.1", only: [:dev, :test], runtime: false},
Expand Down
Loading

0 comments on commit c82adb8

Please sign in to comment.