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 38d7e23
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 24 deletions.
2 changes: 1 addition & 1 deletion backend/lib/edgehog/groups/device_group/device_group.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2022-2024 SECO Mind Srl
# Copyright 2022-2025 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2024 SECO Mind Srl
# Copyright 2025 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2024 SECO Mind Srl
# Copyright 2025 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#
# This file is part of Edgehog.
#
# Copyright 2025 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
if missing_target_ids?(error) do
%{
error
| fields: [:target_group_ids],
message: "One or more target groups could not be found"
}
else
error
end
end

defp missing_target_ids?(error) do
error[:code] == "not_found"
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2023-2024 SECO Mind Srl
# Copyright 2023-2025 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.
Expand All @@ -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
4 changes: 2 additions & 2 deletions backend/mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2024 SECO Mind Srl
# Copyright 2021-2025 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.
Expand Down 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
20 changes: 10 additions & 10 deletions backend/mix.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2023-2024 SECO Mind Srl
# Copyright 2023-2025 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.
Expand Down Expand Up @@ -177,8 +177,8 @@ defmodule EdgehogWeb.Schema.Mutation.CreateUpdateChannelTest do

assert %{
path: ["createUpdateChannel"],
fields: [:id],
message: "could not be found",
fields: [:target_group_ids],
message: "One or more target groups could not be found",
code: "not_found"
} = error
end
Expand Down Expand Up @@ -206,7 +206,7 @@ defmodule EdgehogWeb.Schema.Mutation.CreateUpdateChannelTest do
code: "invalid_attribute"
} = error

assert name == "\"#{target_group.name}\""
assert name == ~s["#{target_group.name}"]
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2023-2024 SECO Mind Srl
# Copyright 2023-2025 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.
Expand Down Expand Up @@ -154,9 +154,9 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateUpdateChannelTest do

assert %{
path: ["updateUpdateChannel"],
fields: [:id],
fields: [:target_group_ids],
code: "not_found",
message: "could not be found"
message: "One or more target groups could not be found"
} = error
end

Expand All @@ -180,7 +180,7 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateUpdateChannelTest do
message: "The update channel is already set for the device group " <> name
} = error

assert name == "\"#{target_group.name}\""
assert name == ~s["#{target_group.name}"]
end
end

Expand Down

0 comments on commit 38d7e23

Please sign in to comment.