Skip to content

Commit

Permalink
fix: handle api error gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Mar 23, 2024
1 parent 9e249b9 commit 21b3ba1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/api/rest/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,17 @@ func handlerGetAddressDetails(c *fiber.Ctx) error {
params := new(AddressesQuery)
if err := c.QueryParser(params); err != nil {
zap.S().Warnf("Addresses Get Handler ERROR: %s", err.Error())

c.Status(422)
return c.SendString(`{"error": "could not parse query parameters"}`)
}

// Get Addresses
address, err := crud.GetAddressCrud().SelectOne(
addressString,
)
address, err := crud.GetAddressCrud().SelectOne(addressString)
if err != nil {
if err == crud.ErrRecordNotFound {
c.Status(404)
return c.SendString(`{"error": "address not found"}`)
}
c.Status(500)
zap.S().Warn(
"Endpoint=handlerGetAddressDetails",
Expand All @@ -179,7 +180,6 @@ func handlerGetAddressDetails(c *fiber.Ctx) error {
if err != nil {
return c.SendString(`{"error": "parsing error"}`)
}

return c.SendString(string(body))
}

Expand Down
7 changes: 6 additions & 1 deletion src/crud/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package crud

import "reflect"
import (
"errors"
"reflect"
)

var ErrRecordNotFound = errors.New("record not found")

func extractFilledFieldsFromModel(modelValueOf reflect.Value, modelTypeOf reflect.Type) map[string]interface{} {

Expand Down

0 comments on commit 21b3ba1

Please sign in to comment.