Return JSON-encoded errors

This commit is contained in:
2022-08-28 15:07:46 +02:00
parent 9da60fa4e7
commit 0110d6994b
5 changed files with 52 additions and 7 deletions

View File

@@ -2,14 +2,17 @@ package main
import (
"flag"
"net/http"
"5feet11/internal/config"
"5feet11/internal/errorx"
"5feet11/internal/handler"
"5feet11/internal/svc"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/rest/httpx"
)
var configFile = flag.String("f", "etc/fivefeeteleven-api.yaml", "the config file")
@@ -26,6 +29,15 @@ func main() {
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
httpx.SetErrorHandler(func(err error) (int, interface{}) {
switch e := err.(type) {
case *errorx.CodeError:
return http.StatusBadRequest, e.Data()
default:
return http.StatusInternalServerError, nil
}
})
logx.Infof("Starting server at http://%s:%d", c.Host, c.Port)
server.Start()
}