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

Bad error message when sending the wrong thing through a port: [object Object] #1043

Open
lydell opened this issue Sep 16, 2019 · 0 comments

Comments

@lydell
Copy link

lydell commented Sep 16, 2019

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant