Custom errors don't display correctly in the web shell

Hi,
Not sure if this is a bug.
I’m trying to get the error value to show with an If statement when a user exists, but I get an undefined.

Email elready exists in collection users.
This returns an undefined when called:

Query(
  Lambda(
    ["email", "password", "userName"],
    Let(
      {
        exists: Exists(Match(Index("usersByEmail"), Var("email"))),
        user: If(
          Not(Var("exists")),
          Create(Collection("users"), {
            credentials: { password: Var("password") },
            data: { email: Var("email"), userName: Var("userName") }
          }),
          "user already exists"
        )
      },
      { user: Var("user"), error: If(Var("exists"), "user exists", null) }
    )
  )
)
returns:
undefined

Though this works strangely, if the string is empty on the If expression.

Query(
  Lambda(
    ["email", "password", "userName"],
    Let(
      {
        exists: Exists(Match(Index("usersByEmail"), Var("email"))),
        user: If(
          Not(Var("exists")),
          Create(Collection("users"), {
            credentials: { password: Var("password") },
            data: { email: Var("email"), userName: Var("userName") }
          }),
          "user already exists"
        )
      },
      { user: Var("user"), error: If(Var("exists"), "", null) }
    )
  )
)

returns:
{
  user: "user already exists",
  error: ""
}

Any help is appreciated.

@roopsBee This is a bug in WebShell. I will raise a ticket to fix it.

WebShell
image

Fauna Shell
image

Not sure if this is related, but from what it looks like it might.

I was having a similar issue with my function:

Query(
  Lambda(
    "id",
    Let(
      {
        placeRef: Call(Function("getPlaceRefById"), Var("id")),
      },
      If(
        IsNull(Var("placeRef")),
        {
          error: "placeDoesNotExist",
          id: Var("id")
        },
        Let(
          {
            placeDetailsSetRef: Match(Index("place_details_by_placeId"), Var("placeRef"))
          },
          If(
            Exists(Var("placeDetailsSetRef")),
            Merge(
              Select("data", Get(Var("placeRef"))),
              Select("data", Get(Var("placeDetailsSetRef")))
            ),
            {
              error: "placeDetailsDoNotExist",
              googleId: Var("id")
            }
          )
        )
      )
    )
  )
)

The thing to focus on here is that whenever the function had to return the anonymous object {error: '...', id: ref(...)}, it actually returned undefined.

I managed to resolve the issue by renaming the error property to something else. Is this property name reserved and can’t be used in custom objects?

Hi @robert. This only happens in the web shell and how it interprets results to display them in the results window. You are free to return an object with any fields you wish.

It’s currently an obscure feature, again only for the web shell. If your results contain an error field that is “truthy” the webshell will display the message field in your result.

Your query result displays undefined because your result does not contain a message field (in javascript that means message is undefined). I emphasize that undefined is only “displayed” – if you check your network tab, the query still returns the object you are expecting.

image

But again, this is only how the web shell behaves. If I run in the same query with the CLI I get the plain object as a result. Same as with any of the drivers.

image

The argument that “it’s a feature, not a bug!” only takes you so far :relaxed: If folks are finding this confusing, then it deserves another look. I’ve reopened that ticket that Jay originally submitted.

2 Likes

The web shell has been updated to handle responses from Fauna that happen to contain an error field. The web shell is now consistent with the drivers and Fauna Shell CLI.

image