I am writing a simple rest api using echo framework for route handling. I am trying to maintain centralised error handling using middlewares. In the following code, in the error method implementation I want to return a struct so that I can use that info in custom error Handler
main.go
package main
import log "github.com/sirupsen/logrus"
import "github.com/labstack/echo"
import "net/http"
import "fmt"
func main(){
e := echo.New()
e.GET("process", PostHandler)
e.HTTPErrorHandler = customHTTPErrorHandler
log.Fatal(e.Start(":3000"))
}
func PostHandler(ctx echo.Context) error{
x:= 0;
if x != 0 {
return NewTypeError(1024, "Invalid arguments")
}
return ctx.JSON(http.StatusOK, "message")
}
func customHTTPErrorHandler(err error, c echo.Context) {
fmt.Println("Inside custom error")
fmt.Println(err);
}
error.go
package main
import "fmt"
type Error struct{
Message string
Internal int
}
func (e *Error)Error() string{
fmt.Println("Hello error")
return "error"
}
func NewTypeError( Internal int, Message string) *Error {
fmt.Println(Internal)
fmt.Println(Message)
return &Error{
Message,
Internal,
}
}
I want my output json response to be sent from custom error middleware like this.
{
code: "1024",
message: "Invalid Arguments"
}