Use JSON for errors
This commit is contained in:
parent
123404d090
commit
c1af91b0e4
@ -1,9 +1,8 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"strings"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
@ -14,33 +13,8 @@ func httpErrorHandler(err error, c echo.Context) {
|
|||||||
if httpError, ok := err.(*echo.HTTPError); ok {
|
if httpError, ok := err.(*echo.HTTPError); ok {
|
||||||
code = httpError.Code
|
code = httpError.Code
|
||||||
}
|
}
|
||||||
filePath := fmt.Sprintf("public/%d.html", code)
|
if strings.Contains(err.Error(), "violates check constraint") {
|
||||||
var f *os.File
|
code = 400
|
||||||
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.JSON(code, map[string]any{"status": code})
|
||||||
c.Logger().Error(err)
|
|
||||||
serveError(c, 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user