Better error messages

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Better error messages

Peter Hall
If I have something like this

main = do
    args <- getArgs
    let file = args !! 0
    -- etc...


And I run it without any arguments, I get an error message like this:
"Prelude.(!!): index too large".

What's the best way to handle an error like this to give a better
message back to the user?


Peter


Reply | Threaded
Open this post in threaded view
|

Better error messages

Daniel Fischer
On Thursday 29 December 2011, 21:25:31, Peter Hall wrote:

> If I have something like this
>
> main = do
>     args <- getArgs
>     let file = args !! 0
>     -- etc...
>
>
> And I run it without any arguments, I get an error message like this:
> "Prelude.(!!): index too large".
>
> What's the best way to handle an error like this to give a better
> message back to the user?

main = do
    args <- getArgs
    case args of
      [] -> complain
      _ -> happiness

or

    args <- getArgs
    if null args
      then complain
      else happiness


Reply | Threaded
Open this post in threaded view
|

Better error messages

Chaddaï Fouché
In reply to this post by Peter Hall
On Thu, Dec 29, 2011 at 9:25 PM, Peter Hall <peter.hall at memorphic.com> wrote:

> If I have something like this
>
> main = do
> ? ?args <- getArgs
> ? ?let file = args !! 0
> ? ?-- etc...
>
>
> And I run it without any arguments, I get an error message like this:
> "Prelude.(!!): index too large".
>
> What's the best way to handle an error like this to give a better
> message back to the user?

Don't use partial functions (functions that can fail on some input)
except if you're sure they won't fail (and even then you may be better
off not using them...), here you can use pattern matching :

> main = do
>    args <- getArgs
>    case args of
>      [file] -> -- etc
>      _ -> error "You should call this program with one file argument !!"

In this particular case (arguments parsing), if you ever find yourself
with more complicated argument handling to do, consider using one of
the good packages that make this easy and safe to do like cmdargs.

--
Jeda?


Reply | Threaded
Open this post in threaded view
|

Better error messages

Michael Orlitzky
In reply to this post by Daniel Fischer
On 12/29/11 15:42, Daniel Fischer wrote:

> On Thursday 29 December 2011, 21:25:31, Peter Hall wrote:
>> If I have something like this
>>
>> main = do
>>     args <- getArgs
>>     let file = args !! 0
>>     -- etc...
>>
>>
>> And I run it without any arguments, I get an error message like this:
>> "Prelude.(!!): index too large".
>>
>> What's the best way to handle an error like this to give a better
>> message back to the user?
>
> main = do
>     args <- getArgs
>     case args of
>       [] -> complain
>       _ -> happiness
>
> or
>
>     args <- getArgs
>     if null args
>       then complain
>       else happiness

I've always used Control.Monad.when here, to avoid unnecessary nesting
(or the alternative "do nothing"). E.g,

  main = do
    args <- getArgs
    when (null args) $ do
      putStrLn help_text
      exitWith $ ExitFailure exit_not_enough_args




Reply | Threaded
Open this post in threaded view
|

Better error messages

Peter Hall
In reply to this post by Daniel Fischer
Thanks,
I was trying this, but it turns out my indentation was wrong; the
'then' and 'else' need to be indented under the 'if', or else it gets
confused about the do block.

Also thanks for the other solutions. I'm looking up cmdargs now.

Peter


On Thu, Dec 29, 2011 at 8:42 PM, Daniel Fischer
<daniel.is.fischer at googlemail.com> wrote:

> On Thursday 29 December 2011, 21:25:31, Peter Hall wrote:
>> If I have something like this
>>
>> main = do
>> ? ? args <- getArgs
>> ? ? let file = args !! 0
>> ? ? -- etc...
>>
>>
>> And I run it without any arguments, I get an error message like this:
>> "Prelude.(!!): index too large".
>>
>> What's the best way to handle an error like this to give a better
>> message back to the user?
>
> main = do
> ? ?args <- getArgs
> ? ?case args of
> ? ? ?[] -> complain
> ? ? ?_ -> happiness
>
> or
>
> ? ?args <- getArgs
> ? ?if null args
> ? ? ?then complain
> ? ? ?else happiness