Use JSON for errors

This commit is contained in:
ekzyis 2023-11-26 18:21:26 +01:00
parent 123404d090
commit c1af91b0e4
1 changed files with 4 additions and 30 deletions

View File

@ -1,9 +1,8 @@
package server
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/labstack/echo/v4"
)
@ -14,33 +13,8 @@ func httpErrorHandler(err error, c echo.Context) {
if httpError, ok := err.(*echo.HTTPError); ok {
code = httpError.Code
}
filePath := fmt.Sprintf("public/%d.html", code)
var f *os.File
if f, err = os.Open(filePath); err != nil {
c.Logger().Error(err)
serveError(c, 500)
return
}
if err = c.Stream(code, "text/html", f); err != nil {
c.Logger().Error(err)
serveError(c, 500)
return
if strings.Contains(err.Error(), "violates check constraint") {
code = 400
}
}
func serveError(c echo.Context, code int) error {
var (
f *os.File
err error
)
if f, err = os.Open(fmt.Sprintf("public/%d.html", code)); err != nil {
c.Logger().Error(err)
return err
}
// TODO return errors in JSON
if err = c.Stream(code, "text/html", f); err != nil {
c.Logger().Error(err)
return err
}
return nil
c.JSON(code, map[string]any{"status": code})
}