# Why does the parser fail to output the error message?

**URL:** <https://discourse.elm-lang.org/t/why-does-the-parser-fail-to-output-the-error-message/9361>\
**Category:** Learn\
**Created:** [September 29, 2023, 9:23pm UTC](https://discourse.elm-lang.org/t/why-does-the-parser-fail-to-output-the-error-message/9361 "2023-09-29T21:23:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![polarit](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/polarit/32/4651_2.png) [@polarit](https://discourse.elm-lang.org/u/polarit)\
**Post date:** [September 29, 2023, 9:23pm UTC](https://discourse.elm-lang.org/t/why-does-the-parser-fail-to-output-the-error-message/9361/1 "2023-09-29T21:23:04Z")

</div>

This is probably a trivial issue for an experienced code writer but very frustrating to me as trying to solve it couple of days already.  
.

1. The basic [simple](https://ellie-app.com/nYp2MsSvFcTa1) example  
It works as expected and outputs error messages if the range limits of integer vales are exceeded.

2. The case [extended](https://ellie-app.com/p32DVYtmTWma1) with elm browser.sandbox model  
The string to be parsed, is input by the user.  
This will too react to the exceeding of the ranges. However, it will output the zero location (lat 0, lon 0) as the `Result.withDefault` given by the parser instead of error message. It seems to skip using the function `rangeProblem` where the range limits are used.

---

<div class="post-metadata">

**Author:** ![spruce\_moose](https://avatars.discourse-cdn.com/v4/letter/s/ed8c4c/32.png) [@spruce\_moose](https://discourse.elm-lang.org/u/spruce_moose)\
**Post date:** [September 30, 2023, 5:13am UTC](https://discourse.elm-lang.org/t/why-does-the-parser-fail-to-output-the-error-message/9361/2 "2023-09-30T05:13:00Z")

</div>

Your `rangeProblem` functions are not being skipped - using `Result.withDefault` is throwing away your error messages. The reason it is working in the first example is because you are using `Debug.toString` to unwrap the result (you can see this happening when it says either “Ok” or “Err” after the “=\>” in the output).

For `Parser.run geoLocation v1` ([Parser.run docs](https://package.elm-lang.org/packages/elm/parser/latest/Parser#run)), the return type is of `Result (List DeadEnd) GeoLocation`. When you apply the result of this to `Result.withDefault zero` you are basically saying if the DeadEnd list has any errors, throw them away and replace the result with the `zero` geolocation.

You need to keep the results from the parser wrapped in the `Result` so that you can then access either the Geolocation or the errors with pattern matching like below.

```elm
getLocation inputS =
  case parseLocation inputS of
      Ok geolocation -> 
          "Latitude " ++ String.fromFloat (Tuple.first geolocation)
              ++ " Longitude " ++ String.fromFloat (Tuple.second geolocation)
      Err deadEnds ->
         Debug.toString deadEnds

```

I’d recommend trying to write your first example without using `Debug.toString`. It should hopefully make what I’ve written above a little clearer.

Just watch out for using [Parser.deadEndsToString](https://package.elm-lang.org/packages/elm/parser/latest/Parser#deadEndsToString) because it is not implemented. You’ll need to map over the DeadEnds yourself.

---

<div class="post-metadata">

**Author:** ![spruce\_moose](https://avatars.discourse-cdn.com/v4/letter/s/ed8c4c/32.png) [@spruce\_moose](https://discourse.elm-lang.org/u/spruce_moose)\
**Post date:** [September 30, 2023, 5:25am UTC](https://discourse.elm-lang.org/t/why-does-the-parser-fail-to-output-the-error-message/9361/3 "2023-09-30T05:25:38Z")

</div>

Did some Googling and instead of using Parser.deadEndsToString, you could copy the `deadEndToString` and `problemToString` functions from the (5 year old) [pull request that implements them](https://github.com/elm/parser/pull/16/files).

---

<div class="post-metadata">

**Author:** ![polarit](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/polarit/32/4651_2.png) [@polarit](https://discourse.elm-lang.org/u/polarit)\
**Post date:** [September 30, 2023, 7:23am UTC](https://discourse.elm-lang.org/t/why-does-the-parser-fail-to-output-the-error-message/9361/4 "2023-09-30T07:23:49Z")

</div>

> [@spruce\_moose](#):
>
> Just watch out for using [Parser.deadEndsToString](https://package.elm-lang.org/packages/elm/parser/latest/Parser#deadEndsToString) because it is not implemented.

Thank you for your thorough explanation. I believe, this will help me a lot, how to go ahead. Yes, it’s a good idea, working at first with the simple example 1 and ofcourse, I must get rid of all `Debug.toString`references in the final version.

I had read earlier the parser package docs but still missed the info, what to do with `deadEnds`.

---

<div class="post-metadata">

**Author:** ![polarit](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/polarit/32/4651_2.png) [@polarit](https://discourse.elm-lang.org/u/polarit)\
**Post date:** [September 30, 2023, 7:26am UTC](https://discourse.elm-lang.org/t/why-does-the-parser-fail-to-output-the-error-message/9361/5 "2023-09-30T07:26:47Z")

</div>

Ok, I shall at this alternative as well.

---

<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:** [October 10, 2023, 7:27am UTC](https://discourse.elm-lang.org/t/why-does-the-parser-fail-to-output-the-error-message/9361/6 "2023-10-10T07:27:35Z")

</div>

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