SSCCE
https://ellie-app.com/6G4Vt26ggBXa1
port module Main exposing (main)
main : Program () () ()
main =
Platform.worker
{ init = always ( (), Cmd.none )
, update = \msg model -> ( model, Cmd.none )
, subscriptions = always (test (always ()))
}
port test : (String -> msg) -> Sub msg
<body>
<script>
var app = Elm.Main.init()
app.ports.test.send(1337)
</script>
</body>
Result:
Error: Trying to send an unexpected type of value through port `test`: [object Object]
Expected something like this:
Error: Trying to send an unexpected type of value through port `test`: Expected a STRING, but got: 1337
Story
If I have a port like this…
port test : (String -> msg) -> Sub msg
…and accidentally send a number instead of a string…
app.ports.test.send(1337)
…I get this error:
Error: Trying to send an unexpected type of value through port `test`: [object Object]
It seems like no matter what type of faulty value I send, it always says [object Object]. Which isn’t very helpful :)
The browser devtools shows not only the error message, but also which piece of code caused the error. If I click the error location, I’m taken to code looking like this in the devtools debugger:
case 4:
var portName = fact1;
var problem = fact2;
throw new Error('Trying to send an unexpected type of value through port `' + portName + '`:\n' + problem);
That comes from here:
|
case 4: |
|
var portName = fact1; |
|
var problem = fact2; |
|
throw new Error('Trying to send an unexpected type of value through port `' + portName + '`:\n' + problem); |
If I put a breakpoint on the throw line, it turns out that problem/fact2 isn’t a string, but an object looking like this:
{
"$": "Failure",
"a": "Expecting a STRING",
"b": {
"$": 0,
"a": 1337
}
}
That explains where [object Object] is coming from. So it looks like it would be possible to say something like Expecting a STRING, but got: 1337 instead.
Background
The other day I was trying to make a port like this:
port clickedOutsideDropdown : (() -> msg) -> Sub msg
The JavaScript code only has to tell Elm that a click occurred, but does not need to pass any extra information. That’s why I used unit/() in the port.
When it was time to implement the JavaScript part, I was thinking: “Since this port does not take any value, do I need to pass anything to .send?” So I tried this…
app.ports.clickedOutsideDropdown.send()
…which gave me this error:
Error: Trying to send an unexpected type of value through port `test`: [object Object]
I was confused. I didn’t send anything, so what [object Object] is the error message talking about?
After using the good old JavaScript debugger I finally figured out that I was supposed to pass null:
app.ports.clickedOutsideDropdown.send(null)
SSCCE
https://ellie-app.com/6G4Vt26ggBXa1
Result:
Expected something like this:
Story
If I have a port like this…
…and accidentally send a number instead of a string…
…I get this error:
It seems like no matter what type of faulty value I send, it always says
[object Object]. Which isn’t very helpful :)The browser devtools shows not only the error message, but also which piece of code caused the error. If I click the error location, I’m taken to code looking like this in the devtools debugger:
That comes from here:
core/src/Elm/Kernel/Debug.js
Lines 257 to 260 in fc9d96b
If I put a breakpoint on the
throwline, it turns out thatproblem/fact2isn’t a string, but an object looking like this:{ "$": "Failure", "a": "Expecting a STRING", "b": { "$": 0, "a": 1337 } }That explains where
[object Object]is coming from. So it looks like it would be possible to say something likeExpecting a STRING, but got: 1337instead.Background
The other day I was trying to make a port like this:
The JavaScript code only has to tell Elm that a click occurred, but does not need to pass any extra information. That’s why I used unit/
()in the port.When it was time to implement the JavaScript part, I was thinking: “Since this port does not take any value, do I need to pass anything to
.send?” So I tried this……which gave me this error:
I was confused. I didn’t send anything, so what
[object Object]is the error message talking about?After using the good old JavaScript debugger I finally figured out that I was supposed to pass
null: