Skip to content

Commit

Permalink
Tidy up handling errors so things work
Browse files Browse the repository at this point in the history
  • Loading branch information
mikogs committed Dec 27, 2024
1 parent cb9f979 commit f89c416
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 31 deletions.
4 changes: 2 additions & 2 deletions err.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ type ErrController struct {
Err error
}

func (e *ErrController) Error() string {
func (e ErrController) Error() string {
return e.Err.Error()
}

func (e *ErrController) Unwrap() error {
func (e ErrController) Unwrap() error {
return e.Err
}

Expand Down
12 changes: 6 additions & 6 deletions internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

// initHelpers creates all the Struct2sql objects. For HTTP endpoints, it is necessary to create these first
func (c *Controller) initHelpers(newObjFunc func() interface{}, options HandlerOptions) *ErrController {
func (c *Controller) initHelpers(newObjFunc func() interface{}, options HandlerOptions) error {
obj := newObjFunc()

var forceName string
Expand All @@ -15,7 +15,7 @@ func (c *Controller) initHelpers(newObjFunc func() interface{}, options HandlerO

cErr := c.orm.RegisterStruct(obj, nil, false, forceName, false)
if cErr != nil {
return &ErrController{
return ErrController{
Op: "RegisterStruct",
Err: fmt.Errorf("Error adding SQL generator: %w", cErr.Unwrap()),
}
Expand All @@ -24,7 +24,7 @@ func (c *Controller) initHelpers(newObjFunc func() interface{}, options HandlerO
if options.CreateConstructor != nil {
cErr = c.orm.RegisterStruct(options.CreateConstructor(), obj, false, "", true)
if cErr != nil {
return &ErrController{
return ErrController{
Op: "RegisterStruct",
Err: fmt.Errorf("Error adding SQL generator: %w", cErr.Unwrap()),
}
Expand All @@ -34,7 +34,7 @@ func (c *Controller) initHelpers(newObjFunc func() interface{}, options HandlerO
if options.ReadConstructor != nil {
cErr = c.orm.RegisterStruct(options.ReadConstructor(), obj, false, "", true)
if cErr != nil {
return &ErrController{
return ErrController{
Op: "RegisterStruct",
Err: fmt.Errorf("Error adding SQL generator: %w", cErr.Unwrap()),
}
Expand All @@ -44,7 +44,7 @@ func (c *Controller) initHelpers(newObjFunc func() interface{}, options HandlerO
if options.UpdateConstructor != nil {
cErr = c.orm.RegisterStruct(options.UpdateConstructor(), obj, false, "", true)
if cErr != nil {
return &ErrController{
return ErrController{
Op: "RegisterStruct",
Err: fmt.Errorf("Error adding SQL generator: %w", cErr.Unwrap()),
}
Expand All @@ -54,7 +54,7 @@ func (c *Controller) initHelpers(newObjFunc func() interface{}, options HandlerO
if options.ListConstructor != nil {
cErr = c.orm.RegisterStruct(options.ListConstructor(), obj, false, "", true)
if cErr != nil {
return &ErrController{
return ErrController{
Op: "RegisterStruct",
Err: fmt.Errorf("Error adding SQL generator: %w", cErr.Unwrap()),
}
Expand Down
10 changes: 5 additions & 5 deletions internal_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (c Controller) handleHTTPGet(w http.ResponseWriter, r *http.Request, newObj
}
continue
}
if errF.Op == "GetHelper" {
if errF.(ErrController).Op == "GetHelper" {
c.writeErrText(w, http.StatusInternalServerError, "get_helper")
return
} else {
Expand Down Expand Up @@ -293,10 +293,10 @@ func (c Controller) jsonID(id int64) []byte {
return []byte(fmt.Sprintf("{\"id\":\"%d\"}", id))
}

func (c Controller) uriFilterToFilter(obj interface{}, filterName string, filterValue string) (string, interface{}, *ErrController) {
func (c Controller) uriFilterToFilter(obj interface{}, filterName string, filterValue string) (string, interface{}, error) {
fieldName, cErr := c.orm.GetFieldNameFromDBCol(obj, filterName)
if cErr != nil {
return "", nil, &ErrController{
return "", nil, ErrController{
Op: "GetDBCol",
Err: fmt.Errorf("Error getting field name from filter: %w", cErr),
}
Expand All @@ -311,7 +311,7 @@ func (c Controller) uriFilterToFilter(obj interface{}, filterName string, filter
if valueField.Type().Name() == "int" {
filterInt, err := strconv.Atoi(filterValue)
if err != nil {
return "", nil, &ErrController{
return "", nil, ErrController{
Op: "InvalidValue",
Err: fmt.Errorf("Error converting string to int: %w", err),
}
Expand All @@ -321,7 +321,7 @@ func (c Controller) uriFilterToFilter(obj interface{}, filterName string, filter
if valueField.Type().Name() == "int64" {
filterInt64, err := strconv.ParseInt(filterValue, 10, 64)
if err != nil {
return "", nil, &ErrController{
return "", nil, ErrController{
Op: "InvalidValue",
Err: fmt.Errorf("Error converting string to int64: %w", err),
}
Expand Down
7 changes: 0 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,6 @@ func makePUTInsertRequest(j string, status int, t *testing.T) []byte {
t.Fatalf("PUT method failed on HTTP server with handler from GetHTTPHandler: %s", err.Error())
}
if resp.StatusCode != status {
b, _ := io.ReadAll(resp.Body)
log.Printf("url: %s", url)
log.Printf("response body: %s", string(b))
t.Fatalf("PUT method returned wrong status code, want %d, got %d", status, resp.StatusCode)
}

Expand Down Expand Up @@ -361,10 +358,6 @@ func makePUTUpdateRequest(j string, id int64, customURI string, t *testing.T) []
t.Fatalf("PUT method failed on HTTP server with handler from GetHTTPHandler: %s", err.Error())
}
if resp.StatusCode != http.StatusOK {
b, _ := io.ReadAll(resp.Body)
log.Printf("url: %s", url)
log.Printf("response body: %s", string(b))

t.Fatalf("PUT method returned wrong status code, want %d, got %d", http.StatusOK, resp.StatusCode)
}

Expand Down
45 changes: 34 additions & 11 deletions orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package crud

import (
"database/sql"
"errors"

struct2db "github.com/go-phings/struct-db-postgres"
)
Expand All @@ -11,6 +12,8 @@ type ORMError interface {
IsInvalidFilters() bool
// Unwraps unwarps the original error
Unwrap() error
// Error returns error string
Error() string
}

type ORM interface {
Expand Down Expand Up @@ -58,15 +61,15 @@ type ormErrorImpl struct {
err error
}

func (o *ormErrorImpl) IsInvalidFilters() bool {
func (o ormErrorImpl) IsInvalidFilters() bool {
return o.op == "ValidateFilters"
}

func (o *ormErrorImpl) Error() string {
func (o ormErrorImpl) Error() string {
return o.err.Error()
}

func (o *ormErrorImpl) Unwrap() error {
func (o ormErrorImpl) Unwrap() error {
return o.err
}

Expand All @@ -78,19 +81,35 @@ type wrappedStruct2db struct {
}

func (w *wrappedStruct2db) CreateTables(objs ...interface{}) error {
return w.orm.CreateTables(objs...)
err := w.orm.CreateTables(objs...)
if err != nil {
return errors.New("create table failed")
}
return nil
}

func (w *wrappedStruct2db) Load(obj interface{}, id string) error {
return w.orm.Load(obj, id, struct2db.LoadOptions{})
err := w.orm.Load(obj, id, struct2db.LoadOptions{})
if err != nil {
return errors.New("load failed")
}
return nil
}

func (w *wrappedStruct2db) Save(obj interface{}) error {
return w.orm.Save(obj, struct2db.SaveOptions{})
err := w.orm.Save(obj, struct2db.SaveOptions{})
if err != nil {
return errors.New("save failed")
}
return nil
}

func (w *wrappedStruct2db) Delete(obj interface{}) error {
return w.orm.Delete(obj, struct2db.DeleteOptions{})
err := w.orm.Delete(obj, struct2db.DeleteOptions{})
if err != nil {
return errors.New("delete failed")
}
return nil
}

func (w *wrappedStruct2db) Get(newObjFunc func() interface{}, order []string, limit int, offset int, filters map[string]interface{}, rowObjTransformFunc func(interface{}) interface{}) ([]interface{}, ORMError) {
Expand All @@ -103,13 +122,13 @@ func (w *wrappedStruct2db) Get(newObjFunc func() interface{}, order []string, li
})

if err != nil && err.Op == "ValidateFilters" {
return nil, &ormErrorImpl{
return nil, ormErrorImpl{
op: err.Op,
}
}

if err != nil {
return nil, &ormErrorImpl{
return nil, ormErrorImpl{
op: err.Op,
err: err.Err,
}
Expand All @@ -119,7 +138,11 @@ func (w *wrappedStruct2db) Get(newObjFunc func() interface{}, order []string, li
}

func (w *wrappedStruct2db) GetFieldNameFromDBCol(obj interface{}, field string) (string, error) {
return w.orm.GetFieldNameFromDBCol(obj, field)
s, e := w.orm.GetFieldNameFromDBCol(obj, field)
if e != nil {
return "", errors.New("error getting field name from db col")
}
return s, nil
}

func (w *wrappedStruct2db) GetObjIDValue(obj interface{}) int64 {
Expand All @@ -133,7 +156,7 @@ func (w *wrappedStruct2db) ResetFields(obj interface{}) {
func (w *wrappedStruct2db) RegisterStruct(obj interface{}, inheritFromObj interface{}, overwriteExisting bool, forceNameForDB string, useOnlyRootFromInheritedObj bool) ORMError {
err := w.orm.AddSQLGenerator(obj, inheritFromObj, overwriteExisting, forceNameForDB, useOnlyRootFromInheritedObj)
if err != nil {
return &ormErrorImpl{
return ormErrorImpl{
op: err.Op,
err: err.Err,
}
Expand Down

0 comments on commit f89c416

Please sign in to comment.