Skip to content

Commit

Permalink
Move json error into HTTPError.Data
Browse files Browse the repository at this point in the history
This will likely cause some CI failures, as it's likely to cause some
churn in e2e tests.
  • Loading branch information
jannotti committed Jan 5, 2024
1 parent 6ffe184 commit 467c86f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 28 deletions.
24 changes: 6 additions & 18 deletions daemon/algod/api/client/restClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type HTTPError struct {
StatusCode int
Status string
ErrorString string
Data map[string]any
}

// Error formats an error string.
Expand Down Expand Up @@ -120,24 +121,11 @@ func extractError(resp *http.Response) error {
decodeErr := json.Unmarshal(errorBuf, &errorJSON)

var errorString string
var data map[string]any
if decodeErr == nil {
if errorJSON.Data == nil {
// There's no additional data, so let's just use the message
errorString = errorJSON.Message
} else {
// There's additional data, so let's re-encode the JSON response to show everything.
// We do this because the original response is likely encoded with escapeHTML=true, but
// since this isn't a webpage that extra encoding is not preferred.
var buffer strings.Builder
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
encErr := enc.Encode(errorJSON)
if encErr != nil {
// This really shouldn't happen, but if it does let's default to errorBuff
errorString = string(errorBuf)
} else {
errorString = buffer.String()
}
errorString = errorJSON.Message
if errorJSON.Data != nil {
data = *errorJSON.Data
}
} else {
errorString = string(errorBuf)
Expand All @@ -149,7 +137,7 @@ func extractError(resp *http.Response) error {
return unauthorizedRequestError{errorString, apiToken, resp.Request.URL.String()}
}

return HTTPError{StatusCode: resp.StatusCode, Status: resp.Status, ErrorString: errorString}
return HTTPError{StatusCode: resp.StatusCode, Status: resp.Status, ErrorString: errorString, Data: data}
}

// stripTransaction gets a transaction of the form "tx-XXXXXXXX" and truncates the "tx-" part, if it starts with "tx-"
Expand Down
2 changes: 1 addition & 1 deletion data/transactions/logic/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ func (cx *EvalContext) evalError(err error) error {
"group-index", cx.groupIndex,
"eval-states", cx.evalStates())
if cx.runMode == ModeApp {
details = fmt.Sprintf("app=%d %s", cx.appID, details)
details = fmt.Sprintf("app=%d, %s", cx.appID, details)
err = basics.Annotate(err, "app-index", cx.appID)
}

Expand Down
14 changes: 5 additions & 9 deletions test/e2e-go/restAPI/other/appsRestAPI_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/transactions"
"github.com/algorand/go-algorand/data/transactions/logic"
"github.com/algorand/go-algorand/protocol"
"github.com/algorand/go-algorand/test/framework/fixtures"
"github.com/algorand/go-algorand/test/partitiontest"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -319,15 +318,12 @@ end:
e := err.(client.HTTPError)
a.Equal(400, e.StatusCode)

var er *model.ErrorResponse
err = protocol.DecodeJSON([]byte(e.ErrorString), &er)
a.NoError(err)
a.Equal("Result limit exceeded", er.Message)
a.Equal(uint64(100000), ((*er.Data)["max-api-box-per-application"]).(uint64))
a.Equal(requestedMax, ((*er.Data)["max"]).(uint64))
a.Equal(expectedCount, ((*er.Data)["total-boxes"]).(uint64))
a.Equal("Result limit exceeded", e.ErrorString)
a.EqualValues(100000, e.Data["max-api-box-per-application"])
a.EqualValues(requestedMax, e.Data["max"])
a.EqualValues(expectedCount, e.Data["total-boxes"])

a.Len(*er.Data, 3, fmt.Sprintf("error response (%v) contains unverified fields. Extend test for new fields.", *er.Data))
a.Len(e.Data, 3, fmt.Sprintf("error response (%v) contains unverified fields. Extend test for new fields.", e.Data))
}

// `assertBoxCount` sanity checks that the REST API respects `expectedCount` through different queries against app ID = `createdAppID`.
Expand Down

0 comments on commit 467c86f

Please sign in to comment.