Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest better error message on on_error , if action does not exist. #116

Open
zer3fD opened this issue Feb 5, 2025 · 0 comments
Open
Labels
enhancement New feature or request

Comments

@zer3fD
Copy link

zer3fD commented Feb 5, 2025

Problem

In oban resource, While setting up triggers with on_error function, if the action (update or destroy ) does not exist, getting this error:

key :transaction? not found in: nil.

This error message is hard to debug the issue, since neither trigger nor oban accept transaction? as key.

Here are the total details:


defmodule Mgx.AuditLogs.Workers.CreateAuditLog do
  use Ash.Resource,
    data_layer: AshPostgres.DataLayer,
    domain: Mgx.AuditLogs,
    extensions: [AshOban],
    otp_app: :mgx

  postgres do
    table "audit_logs_background_jobs"
    repo Mgx.RepoBackgroundsJobs
  end

  oban do
    domain Mgx.AuditLogs

    triggers do
      trigger :add_audit_log do
        action :process
        where expr(state == :pending or state == :failed)
        on_error(:failed)
        
        

        log_final_error?(true)
      end
    end
  end

  actions do
    defaults [:read, :destroy, create: :*, update: :*]

    default_accept [
      :state,
      :actor_id,
      :actor_type,
      :action,
      :resource_id,
      :resource_type,
      :created_at
    ]

    update :process do
      accept [:state]

      change set_attribute(:state, :added)

      description "Mark the background job as processed. This is will run with oban triggers."

      change after_transaction(fn _changeset, {:ok, result}, _context ->
               Mgx.AuditLogs.create_audit_log(
                 actor_id: result.actor_id,
                 actor_type: result.actor_type,
                 action: result.action,
                 resource_id: result.resource_id,
                 resource_type: result.resource_type,
                 created_at: result.created_at
               )
             end)
    end
  end

  attributes do
    uuid_primary_key :id

    attribute :state, :atom do
      allow_nil? false

      description "The state of the background job."

      constraints one_of: [:pending, :added, :failed]

      default :pending
    end

    attribute :actor_id, :uuid do
      allow_nil? false

      description "The id of the actor that has performed the action."
    end

    attribute :actor_type, :string do
      allow_nil? false

      description "The type of the actor: e.g 'user', 'system'."
    end

    attribute :action, :string do
      allow_nil? false

      description "The action performed. Example: 'create', 'update', 'delete'."
    end

    attribute :resource_id, :uuid do
      allow_nil? false

      description "The id of the resource on which the action was performed."
    end

    attribute :resource_type, :string do
      allow_nil? false

      description "The type of the resource."
    end

    attribute :created_at, :utc_datetime do
      allow_nil? false

      description "The timestamp when the action was performed."
    end

    timestamps()
  end
end

The compilation error:

== Compilation error in file lib/mgx/audit_logs/workers/create_audit_log.ex ==
** (RuntimeError) Exception in transformer AshOban.Transformers.DefineSchedulers on Mgx.AuditLogs.Workers.CreateAuditLog: 

key :transaction? not found in: nil

If you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map
    (ash_oban 0.3.1) lib/transformers/define_schedulers.ex:296: AshOban.Transformers.DefineSchedulers.define_worker/4
    (ash_oban 0.3.1) lib/transformers/define_schedulers.ex:23: anonymous fn/3 in AshOban.Transformers.DefineSchedulers.transform/1
    (elixir 1.18.0) lib/enum.ex:2546: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ash_oban 0.3.1) lib/transformers/define_schedulers.ex:15: AshOban.Transformers.DefineSchedulers.transform/1
    (spark 2.2.40) lib/spark/dsl/extension.ex:639: anonymous fn/4 in Spark.Dsl.Extension.run_transformers/4
    (elixir 1.18.0) lib/enum.ex:4964: Enumerable.List.reduce/3
    (elixir 1.18.0) lib/enum.ex:2600: Enum.reduce_while/3
    /home/user/MGX/lib/mgx/audit_logs/workers/create_audit_log.ex:1: (file)

Thanks to @zachdaniel, for helping me out.

@zer3fD zer3fD added the enhancement New feature or request label Feb 5, 2025
@zachdaniel zachdaniel moved this to Someday in Ash Framework Roadmap Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Someday
Development

No branches or pull requests

1 participant