Response Json Structure with google JSON Style Guide
func main() {
e := echo.New()
e.HTTPErrorHandler = ctx.HTTPErrorHandler
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
return next(ctx.CustomCtx{c})
}
})
e.GET("/", func(c echo.Context) error {
return c.(ctx.CustomCtx).Resp(http.StatusOK).Data("Hello, World!").Do()
})
e.GET("v2", func(c echo.Context) error {
return c.(ctx.CustomCtx).Resp(http.StatusOK).Ver("v2").Data().Do()
})
e.GET("v3", func(c echo.Context) error {
return c.(ctx.CustomCtx).Resp(http.StatusOK).Ver("v3").Data().Do()
})
e.GET("/error", func(c echo.Context) error {
errCode := 40000001
errMsg := "Error Title"
errDate := ctx.NewErrors()
errDate.Add("Error Message 1")
errDate.Add("Error Message 2")
return c.(ctx.CustomCtx).Resp(errCode).Error(fmt.Sprintf("%v", errMsg)).Code(errCode).Errors(errDate.Error()).Do()
})
e.Logger.Fatal(e.Start(":8080"))
}
{
"apiVersion": "v1",
"data": "Hello, World!"
}
{
"apiVersion": "1.0",
"data": []
}
ref : https://google.github.io/styleguide/jsoncstyleguide.xml?showone=error#error
{
"apiVersion": "1.0",
"error": {
"code": 40000001,
"message": "Err Title",
"errors": [
"Error Message 1",
"Error Message 2"
]
}
}
{
"apiVersion": "1.0",
"error": {
"code": 404,
"message": "Not Found"
}
}