# Making a query parser fail

**URL:** <https://discourse.elm-lang.org/t/making-a-query-parser-fail/3996>\
**Category:** Learn\
**Created:** [July 16, 2019, 6:59am UTC](https://discourse.elm-lang.org/t/making-a-query-parser-fail/3996 "2019-07-16T06:59:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marreman](https://avatars.discourse-cdn.com/v4/letter/m/2bfe46/32.png) [@marreman](https://discourse.elm-lang.org/u/marreman)\
**Post date:** [July 16, 2019, 6:59am UTC](https://discourse.elm-lang.org/t/making-a-query-parser-fail/3996/1 "2019-07-16T06:59:28Z")

</div>

Can I somehow make a query parser fail instead of returning maybes like it does for path segments?

```
type Route
    = VerifyEmail String

routeParser : Parser (Maybe Route -> a) a
routeParser =
    let
        toRoute mode oobCode =
            case mode of
                "verifyEmail" ->
                    Just <| VerifyEmail oobCode

                _ ->
                    Nothing
    in
    Parser.top
        <?> Query.map2 (Maybe.map2 toRoute) (Query.string "mode") (Query.string "oobCode")
        |> Parser.map (Maybe.andThen identity)

```

Something that would turn

`routeParser : Parser (Maybe Route -> a) a`

into

`routeParser : Parser (Route -> a) a`

It seems `Parser.Query.string` should be called `Parser.Query.maybeString` if the presence of the query param is optional?

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [July 16, 2019, 8:25am UTC](https://discourse.elm-lang.org/t/making-a-query-parser-fail/3996/2 "2019-07-16T08:25:54Z")

</div>

It doesn’t look like it, as there is no `fail` operator in the API. You might have to change:

> [@marreman](#):
>
> ```
> type Route = VerifyEmail String
> 
> ```
> 
> to:
> 
> ```
> type Route = VerifyEmail (Maybe String)
> 
> ```

and reject the route based on the `Maybe` after it has been parsed. Not ideal…

---

<div class="post-metadata">

**Author:** ![marreman](https://avatars.discourse-cdn.com/v4/letter/m/2bfe46/32.png) [@marreman](https://discourse.elm-lang.org/u/marreman)\
**Post date:** [July 16, 2019, 9:06am UTC](https://discourse.elm-lang.org/t/making-a-query-parser-fail/3996/3 "2019-07-16T09:06:37Z")

</div>

Yeah, that’s what I ended up doing. I guess one could use elm/parser for more customisability.

---

<div class="post-metadata">

**Author:** ![mgold](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mgold/32/284_2.png) [@mgold](https://discourse.elm-lang.org/u/mgold)\
**Post date:** [July 16, 2019, 4:36pm UTC](https://discourse.elm-lang.org/t/making-a-query-parser-fail/3996/4 "2019-07-16T16:36:32Z")

</div>

> “There’s no way to make a parser fail.”

Have you seen `Parser.problem`?

Edit: Oh, _query_ parsers. Maybe `problem` needs to be exposed by them.

---

<div class="post-metadata">

**Author:** ![marreman](https://avatars.discourse-cdn.com/v4/letter/m/2bfe46/32.png) [@marreman](https://discourse.elm-lang.org/u/marreman)\
**Post date:** [July 17, 2019, 6:13am UTC](https://discourse.elm-lang.org/t/making-a-query-parser-fail/3996/5 "2019-07-17T06:13:57Z")

</div>

You’re referring to `problem` in elm/parser? I was looking for something in the elm/url package.

Edit: Sorry didn’t see your edit.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [July 27, 2019, 6:13am UTC](https://discourse.elm-lang.org/t/making-a-query-parser-fail/3996/6 "2019-07-27T06:13:59Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
