Dolfik1/Funogram

Improve error handling

Closed this issue · 2 comments

Funogram silently prints exception messages from bot to stdout. It's not reliable error handling strategy, so need to be fixed. It possibly would be a breaking change.

https://github.com/Dolfik1/Funogram/blob/master/src/Funogram.Telegram/Bot.fs#L80-L82

A branch-independent link to the code would be

with ex ->
printfn "Internal error: %s" ex.Message
return! loopAsync (offset + 1L)

(which also gets rendered nicely by GitHub)

Regarding the issue itself, I suggest we add an optional API to process these errors. For example, we could pass an optional errorHandler via config, and process the exceptions like so:

            with ex ->
                match config.errorHandler with
                | Some handler -> handler ex
                | None ->
                    printfn "Internal error: %s" ex.Message
                return! loopAsync (offset + 1L)

We could also go further and allow the handler to terminate the bot process. E.g. something like this:

            with ex ->
                let handler = 
                    config.errorHandler
                    |> Option.defaultValue (fun ex loopAsync offset ->
                        printfn "Internal error: %s" ex.Message
                        loopAsync (offset + 1L))
                return! handler ex loopAsync offset

This way, handler will have the choice to either call loopAsync passed to it, or terminate the whole workflow. Also, if the handler in question is optional, then it's not a breaking change. And the handler may be asynchronous, which is interesting (though not so useful maybe).

I have improved error handling in #29