Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLOCK-2308 fix invalid ID and return meanful response to client #44

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions eosws/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,6 @@ func (db *TRXDB) GetTransactions(ctx context.Context, ids []string) (out []*pbco

// ultra-keisuke-kanao --- BLOCK-2123 eosws - runtime error: slice bounds out of range ---
func (db *TRXDB) ListTransactionsForBlockID(ctx context.Context, blockID string, startKey string, limit int) (out *mdl.TransactionList, err error) {
defer func() {
recoveredErr := recover()
if recoveredErr == nil {
return
}

switch v := recoveredErr.(type) {
case error:
err = fmt.Errorf("%w", v)
case string, fmt.Stringer:
err = fmt.Errorf("%s", v)
default:
err = fmt.Errorf("%v", v)
}
}()

if limit < 1 {
return &mdl.TransactionList{
Cursor: opaqueCursor(startKey),
Expand Down
32 changes: 31 additions & 1 deletion eosws/rest/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package rest

import (
"fmt"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -166,6 +167,35 @@ func GetBlockHandler(db eosws.DB) http.Handler {
func GetBlockTransactionsHandler(db eosws.DB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
defer func() {
recoveredErr := recover()
if recoveredErr == nil {
return
}
var errMsg string = "unknown error2"
switch v := recoveredErr.(type) {
case string:
errMsg = v
case error:
errMsg = v.Error()
default:
errMsg = fmt.Sprintf("%v", v)
}
eosws.WriteError(w, r, derr.HTTPInternalServerError(ctx, nil, derr.ErrorCode("unexpected_error"), "An unexpected error occurred.", errMsg))
//////////////////////////////////////////////////////////////////////
// Billable event on REST API endpoint
// WARNING: Ingress / Egress bytess is taken care by the middleware
//////////////////////////////////////////////////////////////////////
dmetering.EmitWithContext(dmetering.Event{
Source: "eosws",
Kind: "REST API - eosq",
Method: "/v0/blocks/{blockID}/transactions",
RequestsCount: 1,
ResponsesCount: 1,
}, ctx)
//////////////////////////////////////////////////////////////////////
return
}()

vars := mux.Vars(r)
id := vars["blockID"]
Expand Down Expand Up @@ -194,7 +224,7 @@ func GetBlockTransactionsHandler(db eosws.DB) http.Handler {

dbTransactionList, err := db.ListTransactionsForBlockID(r.Context(), id, cursor, limit)
if err != nil {
eosws.WriteError(w, r, derr.Wrap(err, "failed to get block transactions"))
eosws.WriteError(w, r, derr.HTTPInternalServerError(ctx, nil, derr.ErrorCode("unexpected_error"), "failed to get block transactions"))
return
}

Expand Down
32 changes: 32 additions & 0 deletions eosws/rest/simplesearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package rest

import (
"fmt"
"net/http"
"net/url"
"regexp"
Expand All @@ -35,6 +36,37 @@ func SimpleSearchHandler(db eosws.DB, blockmetaClient *pbblockmeta.Client) http.
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
query := r.FormValue("q")
defer func() {
recoveredErr := recover()
if recoveredErr == nil {
return
}
var errMsg string = "unknown error1"
switch v := recoveredErr.(type) {
case string:
errMsg = v
case error:
errMsg = v.Error()
default:
errMsg = fmt.Sprintf("%v", v)
}
eosws.WriteError(w, r, derr.HTTPInternalServerError(ctx, nil, derr.ErrorCode("unexpected_error"), "An unexpected error occurred.", errMsg))

//////////////////////////////////////////////////////////////////////
// Billable event on REST API endpoint
// WARNING: Ingress / Egress bytess is taken care by the middleware
//////////////////////////////////////////////////////////////////////
dmetering.EmitWithContext(dmetering.Event{
Source: "eosws",
Kind: "REST API - eosq",
Method: "/v0/simple_search",
RequestsCount: 1,
ResponsesCount: 1,
}, ctx)
//////////////////////////////////////////////////////////////////////
return
}()

if query == "" {
eosws.WriteError(w, r, derr.RequestValidationError(ctx, url.Values{"q": []string{"query parameter should not be empty"}}))
//////////////////////////////////////////////////////////////////////
Expand Down
Loading