isaacseymour / deprecated-time / Time.Iso8601ErrorMsg

A renderer to format error messages resulting from ISO8601 parsing errors.

At this time, there is a renderer, renderText, to render a fixed-font message to, say, a terminal screen. This renderer strives to output the friendly useful error message that elm is famous for.

Main entry

renderText : String -> Parser.Advanced.DeadEnd String Time.Iso8601.Problem -> String

Invoking the renderer. This returns an 'elm compiler-style formatted' error string

import Parser.Advanced exposing (DeadEnd)
import Time.Iso8601 exposing (Problem)

-- The \n in the middle of the string is to provide for the 72-char right margin
failString : String
failString =
    "1991-02-29 is not a valid date"

renderedString : String
renderedString =
    "The 'day-of-month' segment is invalid:\n\n" ++
    "    1991-02-29T12:25:12.0Z\n" ++
    "            ^\n\n" ++
    failString

showError : String -> String
showError input =
   case Time.Iso8601.toDateTime input of
       Ok _ ->
           "success?! what?!"
       Err errors ->
           String.join "\n" <| List.map (renderText input) errors


showError "1991-02-29T12:25:12.0Z"
--> renderedString

Utilities

reflow : String -> String

A convenience function to auto-wrap long strings

Use this method to appropriately wrap the error string returned from renderText above.

-- \n inserted at 72nt position coz is right margin.
reflow "Expecting the value 29 to be in the range 1 to 28 for the specified year, 1991, and month, 2."
--> "Expecting the value 29 to be in the range 1 to 28 for the specified" ++ "\n" ++ "year, 1991, and month, 2."