Skip to content

Commit

Permalink
fix: improve the error logging statements
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Mar 26, 2024
1 parent 1ee7e09 commit 7753db4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
10 changes: 6 additions & 4 deletions src/api/rest/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package rest

import (
"encoding/json"
"errors"
"gorm.io/gorm"
"strconv"

fiber "github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -163,7 +165,7 @@ func handlerGetAddressDetails(c *fiber.Ctx) error {
// Get Addresses
address, err := crud.GetAddressCrud().SelectOne(addressString)
if err != nil {
if err == crud.ErrRecordNotFound {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.Status(404)
return c.SendString(`{"error": "address not found"}`)
}
Expand Down Expand Up @@ -337,7 +339,7 @@ func handlerGetTokenAddresses(c *fiber.Ctx) error {
addressString := c.Params("address")

// Get TokenAddresses
tokenAddresss, err := crud.GetTokenAddressCrud().SelectManyByAddress(addressString)
tokenAddress, err := crud.GetTokenAddressCrud().SelectManyByAddress(addressString)
if err != nil {
c.Status(500)
zap.S().Warn(
Expand All @@ -347,14 +349,14 @@ func handlerGetTokenAddresses(c *fiber.Ctx) error {
return c.SendString(`{"error": "could not retrieve addresses"}`)
}

if len(*tokenAddresss) == 0 {
if len(*tokenAddress) == 0 {
// No Content
c.Status(204)
}

// []models.TokenAddress -> []string
var tokenContractAddresses []string
for _, a := range *tokenAddresss {
for _, a := range *tokenAddress {
tokenContractAddresses = append(tokenContractAddresses, a.TokenContractAddress)
}

Expand Down
19 changes: 14 additions & 5 deletions src/api/rest/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package rest

import (
"encoding/json"
"errors"
"gorm.io/gorm"
"strconv"

fiber "github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -152,10 +154,13 @@ func handlerGetBlockDetails(c *fiber.Ctx) error {

block, err := crud.GetBlockCrud().SelectOne(uint32(number))
if err != nil {
c.Status(404)
return c.SendString(`{"error": "no block found"}`)
if errors.Is(err, gorm.ErrRecordNotFound) {
c.Status(404)
return c.SendString(`{"error": "no block found"}`)
}
c.Status(500)
return c.SendString(`{"error": "could not retrieve block"}`)
}

body, _ := json.Marshal(&block)
return c.SendString(string(body))
}
Expand Down Expand Up @@ -188,8 +193,12 @@ func handlerGetBlockTimestampDetails(c *fiber.Ctx) error {

block, err := crud.GetBlockCrud().SelectOneByTimestamp(uint64(timestamp))
if err != nil {
c.Status(404)
return c.SendString(`{"error": "no block found"}`)
if errors.Is(err, gorm.ErrRecordNotFound) {
c.Status(404)
return c.SendString(`{"error": "no block found"}`)
}
c.Status(500)
return c.SendString(`{"error": "could not retrieve block"}`)
}

body, _ := json.Marshal(&block)
Expand Down
3 changes: 2 additions & 1 deletion src/api/rest/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rest

import (
"encoding/json"
"errors"
"gorm.io/gorm"
"strconv"

Expand Down Expand Up @@ -91,7 +92,7 @@ func handlerGetLogs(c *fiber.Ctx) error {
params.Method,
)
if err != nil {
if err == gorm.ErrRecordNotFound {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.Status(404)
return c.SendString(`{"error": "logs not found"}`)
}
Expand Down
11 changes: 8 additions & 3 deletions src/api/rest/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package rest

import (
"encoding/json"
"errors"
"fmt"
fiber "github.com/gofiber/fiber/v2"
"github.com/sudoblockio/icon-go-api/models"
"go.uber.org/zap"
"gorm.io/gorm"
"strconv"
"sync"

Expand Down Expand Up @@ -231,10 +233,13 @@ func handlerGetTransaction(c *fiber.Ctx) error {

transaction, err := crud.GetTransactionCrud().SelectOne(hash, -1)
if err != nil {
c.Status(404)

if errors.Is(err, gorm.ErrRecordNotFound) {
c.Status(404)
return c.SendString(`{"error": "transaction not found"}`)
}
c.Status(500)
zap.S().Warn(err.Error())
return c.SendString(`{"error": "no transaction found"}`)
return c.SendString(`{"error": "could not retrieve transaction"}`)
}

body, _ := json.Marshal(&transaction)
Expand Down
3 changes: 0 additions & 3 deletions src/crud/utils.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package crud

import (
"errors"
"reflect"
)

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

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

fields := map[string]interface{}{}
Expand Down

0 comments on commit 7753db4

Please sign in to comment.