2021年5月6日星期四

Go Deep understanding of custom error handling in REST API

I am coming from the world of Node.js where I learn a lot from building web application using Express and there, there is a good way, I think, to handle when dealing with error and in case of unexpected error, there is a awesome way to catch it.

So, I looked for the same thing in Go. I don't know if I have already found it, but what I found come from this link https://astaxie.gitbooks.io/build-web-application-with-golang/content/en/11.1.html and from some articles I read, it seems like many developers are using the same approach.

My concern, and not a real one, is that I don't understand some part of that code below.

type appHandler func(http.ResponseWriter, *http.Request) error    func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {      if err := fn(w, r); err != nil {          http.Error(w, err.Error(), 500)      }  }  

I know that it's possible to create custom type in go, but seriously I don't understand what this one means or how to understand it in the http.Serve

type appHandler func(http.ResponseWriter, *http.Request) error  

and one of the thing that I don't catch is

func (fn AppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {  

In most of the code I read, generally, it's a struct or a replaced type (I mean type Account int) but here, it's a function. I would like to understand how is it going to help in handling errors.

Inside the implementation of the ServeHTTP up there, we have this line err := fn(w, r). Please, can you explain it?

From the same article, we have this code

func viewRecord(w http.ResponseWriter, r *http.Request) error {      c := appengine.NewContext(r)      key := datastore.NewKey(c, "Record", r.FormValue("id"), 0, nil)      record := new(Record)      if err := datastore.Get(c, key, record); err != nil {          return err      }      return viewTemplate.Execute(w, record)  }  

and this line

func init() {      http.Handle("/view", appHandler(viewRecord))  }  

Please, could you help me to understand this appHandler(viewRecord)? What is it exactly? It is an instantiation, is it casting? What is it supposed to do? I mean, how to understand that line that seems critical?

One last question, please. Is it possible to catch an error that might happen anywhere during the treatment of a request? In Node.js, you can just do something like

const app = express()    const errorHandler = (    err: Error,    req: Request,    res: Response,    next: NextFunction  ) => {    if (err instanceof CustomError) {      return res.status(err.statusCode).send({ errors: err.serializeErrors() });    }        res.status(500).send({       errors: [{ message: err.message || 'Something went wrong' }]    });  }    app.use(errorHandler())  

Something like that, is it possible in Go?

Thank you.

https://stackoverflow.com/questions/67428860/go-deep-understanding-of-custom-error-handling-in-rest-api May 07, 2021 at 12:05PM

没有评论:

发表评论