notexactlyawe
I can’t seem to pass an anonymous function as an option to a Plug. I am using plug_hmouse which from the documentation takes an option digest that is a function to be called on a string. The example is provided below:
plug PlugHMouse,
... # other options
digest: fn string -> Base.encode16(string) end
However when I try this in my router.ex, I get the following error:
router.ex
plug PlugHMouse,
validate: {"x-hub-signature", webhook_secret},
hash_algo: :sha,
split_digest: true,
digest: fn string -> Base.encode16(string, :lower) end
error
== Compilation error in file lib/spellcanary_web/router.ex ==
** (ArgumentError) cannot escape #Function<0.85932033 in file:lib/spellcanary_web/router.ex>. The supported values are: lists, tuples, maps, atoms, numbers, bitstrings, PIDs and remote functions in the format &Mod.fun/arity
(elixir) src/elixir_quote.erl:122: :elixir_quote.argument_error/1
(elixir) src/elixir_quote.erl:259: :elixir_quote.do_quote/3
(elixir) src/elixir_quote.erl:421: :elixir_quote.do_quote_splice/5
(plug) lib/plug/builder.ex:312: Plug.Builder.init_module_plug/4
(plug) lib/plug/builder.ex:286: anonymous fn/5 in Plug.Builder.compile/3
(elixir) lib/enum.ex:1940: Enum."-reduce/3-lists^foldl/2-0-"/3
(plug) lib/plug/builder.ex:284: Plug.Builder.compile/3
lib/spellcanary_web/router.ex:17: (module)
(stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:208: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
I don’t get this error when passing &Base.encode16/1 but I do get it when using an anonymous function or when defining a function in my router and passing that, eg.
def encode16lower(string) do
Base.encode16(string, :lower)
end
...
plug PlugHMouse,
validate: {"x-hub-signature", webhook_secret},
hash_algo: :sha,
split_digest: true,
digest: &self().encode16lower/1
I need the anonymous function since I want to add an extra argument to the function I’m calling. How can I get around this error?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
Module attributes can not contain anonymous functions. They may only contain a very strict subset of values. Whereas
&Mod.fun/aritis one of these.self()does not return a module name, but a PID. Probably you meant__MODULE__.encode16lower/1?Also Liked
notexactlyawe
Thanks NobbZ, the
__MODULE__tip fixed the error! I didn’t realise that callingplugwould create a module attribute. For anyone reading this thread in future, the docs on Module attributes actually have some info on this.al2o3cr
The issue is that
Plugpre-compiles the stack of plugs, so theinitfunction is called at compile time and its result is stored in the compiled module.As the error message states, that place can’t store anonymous functions.
The “standard idiom” for this situation is to use a tuple to specify a function by name (you’ll see the term “MFA tuple” a lot). Something like:
If
some_funexpects more than one argument, you’d include it in the tuple’s third element.al2o3cr
&SomeModule.other_fun(&1, other_arg)is a shorthand forfn x -> SomeModule.other_fun(x, other_arg) endand will trigger precisely the same problems as any other anonymous function.Despite the similar notation,
&SomeModule.other_fun/1is represented differently internally.Last Post!
budgie
Understood, thank you!