Skip to content

Commit

Permalink
goal: handle not found for looking up AppLocalState with errorAcountN…
Browse files Browse the repository at this point in the history
…otOptedInToApp
  • Loading branch information
cce committed Feb 24, 2022
1 parent 2ee1c5c commit b737a8f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
7 changes: 7 additions & 0 deletions cmd/goal/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ import (
"encoding/base64"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"

"github.com/spf13/cobra"

"github.com/algorand/go-algorand/crypto"
apiclient "github.com/algorand/go-algorand/daemon/algod/api/client"
"github.com/algorand/go-algorand/data/abi"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/transactions"
Expand Down Expand Up @@ -955,6 +958,10 @@ var readStateAppCmd = &cobra.Command{
// Fetching local state. Get account information
ai, err := client.RawAccountApplicationInformation(account, appIdx)
if err != nil {
var httpError apiclient.HTTPError
if errors.As(err, &httpError) && httpError.StatusCode == http.StatusNotFound {
reportErrorf(errorAccountNotOptedInToApp, account, appIdx)
}
reportErrorf(errorRequestFail, err)
}

Expand Down
7 changes: 7 additions & 0 deletions cmd/goal/interact.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ import (
"encoding/base32"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"

"github.com/spf13/cobra"

"github.com/algorand/go-algorand/crypto"
apiclient "github.com/algorand/go-algorand/daemon/algod/api/client"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/transactions"
"github.com/algorand/go-algorand/data/transactions/logic"
Expand Down Expand Up @@ -673,6 +676,10 @@ var appQueryCmd = &cobra.Command{
// Fetching local state. Get account information
ai, err := client.RawAccountApplicationInformation(account, appIdx)
if err != nil {
var httpError apiclient.HTTPError
if errors.As(err, &httpError) && httpError.StatusCode == http.StatusNotFound {
reportErrorf(errorAccountNotOptedInToApp, account, appIdx)
}
reportErrorf(errorRequestFail, err)
}

Expand Down
14 changes: 13 additions & 1 deletion daemon/algod/api/client/restClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ func (e unauthorizedRequestError) Error() string {
return fmt.Sprintf("Unauthorized request to `%s` when using token `%s` : %s", e.url, e.apiToken, e.errorString)
}

// HTTPError is generated when we receive an unhandled error from the server. This error contains the error string.
type HTTPError struct {
StatusCode int
Status string
ErrorString string
}

// Error formats an error string.
func (e HTTPError) Error() string {
return fmt.Sprintf("HTTP %s: %s", e.Status, e.ErrorString)
}

// RestClient manages the REST interface for a calling user.
type RestClient struct {
serverURL url.URL
Expand Down Expand Up @@ -131,7 +143,7 @@ func extractError(resp *http.Response) error {
return unauthorizedRequestError{errorString, apiToken, resp.Request.URL.String()}
}

return fmt.Errorf("HTTP %s: %s", resp.Status, errorString)
return HTTPError{StatusCode: resp.StatusCode, Status: resp.Status, ErrorString: errorString}
}

// stripTransaction gets a transaction of the form "tx-XXXXXXXX" and truncates the "tx-" part, if it starts with "tx-"
Expand Down

0 comments on commit b737a8f

Please sign in to comment.