Error handling

As mentioned in the Go language blog Error handling and Go, error handling is important: "The language's design and conventions encourage you to explicitly check for errors where they occur (as distinct from the convention in other languages of throwing exceptions and sometimes catching them)". Within the Echo framework, you may have noticed that every single handler function returns an error. In the following code, we will explore what happens when a handler returns an error. In this case, we will make a new handler in $GOPATH/src/github.com/PacktPublishing/Echo-Essentials/chapter6/handlers/err.go, which looks like the following:

package handlers

import (
        "errors"

        "github.com/labstack/echo"
)

// Error - Example Error Handler
func Error(c echo.Context) error {
        return errors.New("failure!")
}

When we perform a web service call on the endpoint that routes to this handler code, we get the following:

curl localhost:8080/error -D -
HTTP/1.1 500 Internal Server Error
Content-Type: application/json; charset=UTF-8
Date: Wed, 28 Mar 2018 02:22:04 GMT
Content-Length: 35

{"message":"Internal Server Error"}

Echo is actually interpreting the error we are returning from our handler, and sending an Internal Server Error back to the caller. This is done with the Echo.HTTPErrorHandler method definition on the Echo instance. This error handler takes in an error as a parameter and a context parameter, and performs the conversion from the error from the handler or middleware and converts it to a legitimate response for the user. Within Echo, this method is completely replaceable. By default, it uses the echo.DefaultHTTPErrorHandler, as can be seen at https://github.com/labstack/echo/blob/60f88a7a1c4beaf04b6f9254c8d72b45a2ab161e/echo.go#L317:

With this functionality, you will be able to replace the default error functionality and truly handle errors within your application appropriately. An example of a custom error handler can be seen in the following code:

func myHTTPErrorHandler(err error, c echo.Context) {
code := http.StatusInternalServerError
if httpErr, ok := err.(*echo.HTTPError); ok {
code = httpErr.Code
}
c.String
}

e.HTTPErrorHandler = myHTTPErrorHandler

Even if you do not want to go through the trouble of managing your own custom error type, so long as you return an echo.HTTPError that is fully populated with a status code and message, the echo.DefaultHTTPErrorHandler will take care of your response to the caller appropriately. It will check the error's type in the DefaultHTTPErrorHandler, and if it is of the echo.HTTPError type, Echo will apply the status code and message that was specified within the error definition.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.17.18