diff --git a/common/errors.go b/common/errors.go index 6759947..3d6a5f3 100644 --- a/common/errors.go +++ b/common/errors.go @@ -3,6 +3,7 @@ package common import ( "log" "fmt" + "github.com/gofiber/fiber/v2" ) func Fail(err error, format string, v ...any) error { @@ -10,3 +11,12 @@ func Fail(err error, format string, v ...any) error { log.Printf(err_format, v...) return err } + +func ApiError(c *fiber.Ctx, format string, args ...any) error { + c.Status(500) + + return c.JSON(fiber.Map{ + "error": fmt.Sprintf(format, args...), + }) +} + diff --git a/features/admin/api.go b/features/admin/api.go index 27fefec..ca88757 100644 --- a/features/admin/api.go +++ b/features/admin/api.go @@ -81,7 +81,13 @@ func GetApiInsert(c *fiber.Ctx) error { if err != nil { return c.Redirect("/") } table := c.Params("table") - typeOf := data.Models()[table] + // check that this table exists in the model + typeOf, ok := data.Models()[table] + + if !ok { + return ApiError(c, "admin table %s does not exist", table) + } + result := reflect.New(typeOf) return c.JSON(result.Interface()) diff --git a/features/admin/views.go b/features/admin/views.go index 465a910..d83cbe9 100644 --- a/features/admin/views.go +++ b/features/admin/views.go @@ -38,6 +38,6 @@ func SetupPages(app *fiber.App) { AddAuthedPage(app, true, "admin/table/new/:table/", "admin/table/new") AddPage(app, "admin/", "admin/index") - app.Get("/admin/table/:table/", GetPageContent) + app.Get("admin/table/:table/", GetPageContent) app.Get("/admin/table/:table/:id/", GetPageSelectOne) } diff --git a/static/js/jzed.js b/static/js/jzed.js index cfa53fd..35b1e35 100644 --- a/static/js/jzed.js +++ b/static/js/jzed.js @@ -249,3 +249,14 @@ const $switch = (template_id, data) => { return null; } + +const $no_error = (data, err) => { + if(err === null) { + return true; + } else { + const target = $id('error'); + const templ = $id('error-template'); + $replace_with(target, $render(templ, data)); + return false; + } +} diff --git a/views/admin/table/new.html b/views/admin/table/new.html index 64b6c8e..a9c1ff2 100644 --- a/views/admin/table/new.html +++ b/views/admin/table/new.html @@ -1,19 +1,37 @@