From e43ed299f34ce241072723d3a62ad0b0b14ed32a Mon Sep 17 00:00:00 2001 From: nick Date: Fri, 15 Mar 2024 13:19:09 +0900 Subject: [PATCH] fix: update error handling --- api/adapter/controller.go | 11 ++++++----- api/aggregator/controller.go | 15 ++++++++------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/api/adapter/controller.go b/api/adapter/controller.go index 4018d877a..0c59043c9 100644 --- a/api/adapter/controller.go +++ b/api/adapter/controller.go @@ -1,12 +1,13 @@ package adapter import ( - "bisonai.com/orakl/api/feed" - "bisonai.com/orakl/api/utils" "encoding/json" "fmt" "strconv" + "bisonai.com/orakl/api/feed" + "bisonai.com/orakl/api/utils" + "github.com/ethereum/go-ethereum/crypto" "github.com/go-playground/validator/v10" "github.com/gofiber/fiber/v2" @@ -58,7 +59,7 @@ func insert(c *fiber.Ctx) error { err := computeAdapterHash(payload, true) if err != nil { - panic(err) + return c.Status(fiber.StatusInternalServerError).SendString("failed to compute adapter hash: " + err.Error()) } row, err := utils.QueryRow[AdapterIdModel](c, InsertAdapter, map[string]any{ @@ -105,7 +106,7 @@ func hash(c *fiber.Ctx) error { err = computeAdapterHash(&payload, verify) if err != nil { - panic(err) + return c.Status(fiber.StatusInternalServerError).SendString("failed to compute adapter hash: " + err.Error()) } return c.JSON(payload) } @@ -152,7 +153,7 @@ func computeAdapterHash(data *AdapterInsertModel, verify bool) error { out, err := json.Marshal(input) if err != nil { - panic(err) + return err } hash := crypto.Keccak256Hash([]byte(out)) diff --git a/api/aggregator/controller.go b/api/aggregator/controller.go index 1bd288776..b68863fe5 100644 --- a/api/aggregator/controller.go +++ b/api/aggregator/controller.go @@ -1,13 +1,14 @@ package aggregator import ( + "encoding/json" + "fmt" + "strconv" + "bisonai.com/orakl/api/adapter" "bisonai.com/orakl/api/chain" "bisonai.com/orakl/api/feed" "bisonai.com/orakl/api/utils" - "encoding/json" - "fmt" - "strconv" "github.com/ethereum/go-ethereum/crypto" "github.com/go-playground/validator/v10" @@ -118,7 +119,7 @@ func insert(c *fiber.Ctx) error { } err = computeAggregatorHash(&hashComputeParam, true) if err != nil { - panic(err) + return c.Status(fiber.StatusInternalServerError).SendString("failed to compute aggregator hash: " + err.Error()) } insertParam := _AggregatorInsertModel{ @@ -207,7 +208,7 @@ func hash(c *fiber.Ctx) error { err = computeAggregatorHash(&hashComputeParam, verify) if err != nil { - panic(err) + return c.Status(fiber.StatusInternalServerError).SendString("failed to compute aggregator hash: " + err.Error()) } return c.JSON(hashComputeParam) @@ -313,13 +314,13 @@ func computeAggregatorHash(data *AggregatorHashComputeInputModel, verify bool) e processData := input.AggregatorHashComputeProcessModel out, err := json.Marshal(processData) if err != nil { - panic(err) + return err } hash := crypto.Keccak256Hash([]byte(out)) hashString := fmt.Sprintf("0x%x", hash) if verify && data.AggregatorHash != hashString { - panic(err) + return fmt.Errorf("hashes do not match!\nexpected %s, received %s", hashString, data.AdapterHash) } data.AggregatorHash = hashString