openfaas / golang-http-template

Golang templates for OpenFaaS using HTTP extensions
https://www.openfaas.com/
MIT License
106 stars 57 forks source link

Recover for panics in function Handler for HTTP mode #27

Closed s8sg closed 5 years ago

s8sg commented 5 years ago

In current code we are calling the http function handler as

result, resultErr := function.Handle(req)

In case there is a panic inside the handler the handler server process will exit, failing the future requests for the same.

Optionally it can be handled with a recover as:

defer func() {
       if recoverLog := recover(); recoverLog != nil {
               log.Print(recoverLog)
               w.WriteHeader(http.StatusInternalServerError)
       }
}()
result, resultErr := function.Handle(req)

In this case the handler server keep running

LucasRoesler commented 5 years ago

I like this, is it something you would be willing to also contribute?

s8sg commented 5 years ago

@LucasRoesler Sure

alexellis commented 5 years ago

Hi, thanks for your suggestion.

This is actually by design. I think that it should fail and that the container should be restarted.

If someone has a specific need for a catch-all, then the PR can be forked. I might change my mind in the future, but this is where I am on it at the moment.

Alex

s8sg commented 5 years ago

That make sense. One can handle the panic inside the request handler itself and return a response accordingly. It gives more flexibilities to determine whether to recover or not

alexellis commented 5 years ago

I appreciate the suggestion. Please keep on helping us to make OpenFaaS better 👍

alexellis commented 5 years ago

/add label: wontfix