Skip to content

Commit

Permalink
support debug trace QueryResult (cosmos#11148)
Browse files Browse the repository at this point in the history
Update types/errors/abci.go

Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>

Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>
  • Loading branch information
2 people authored and zakir committed Apr 14, 2022
1 parent 576303b commit 842d5b2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
43 changes: 20 additions & 23 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
// ref: https://github.com/cosmos/cosmos-sdk/pull/8039
defer func() {
if r := recover(); r != nil {
res = sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrPanic, "%v", r))
res = sdkerrors.QueryResultWithDebug(sdkerrors.Wrapf(sdkerrors.ErrPanic, "%v", r), app.trace)
}
}()

Expand All @@ -421,7 +421,7 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {

path := splitPath(req.Path)
if len(path) == 0 {
sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "no query path provided"))
sdkerrors.QueryResultWithDebug(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "no query path provided"), app.trace)
}

switch path[0] {
Expand All @@ -439,7 +439,7 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
return handleQueryCustom(app, path, req)
}

return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown query path"))
return sdkerrors.QueryResultWithDebug(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown query path"), app.trace)
}

// ListSnapshots implements the ABCI interface. It delegates to app.snapshotManager if set.
Expand Down Expand Up @@ -569,12 +569,12 @@ func (app *BaseApp) ApplySnapshotChunk(req abci.RequestApplySnapshotChunk) abci.
func (app *BaseApp) handleQueryGRPC(handler GRPCQueryHandler, req abci.RequestQuery) abci.ResponseQuery {
ctx, err := app.createQueryContext(req.Height, req.Prove)
if err != nil {
return sdkerrors.QueryResult(err)
return sdkerrors.QueryResultWithDebug(err, app.trace)
}

res, err := handler(ctx, req)
if err != nil {
res = sdkerrors.QueryResult(gRPCErrorToSDKError(err))
res = sdkerrors.QueryResultWithDebug(gRPCErrorToSDKError(err), app.trace)
res.Height = req.Height
return res
}
Expand Down Expand Up @@ -754,7 +754,7 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) abci.Res

gInfo, res, err := app.Simulate(txBytes)
if err != nil {
return sdkerrors.QueryResult(sdkerrors.Wrap(err, "failed to simulate tx"))
return sdkerrors.QueryResultWithDebug(sdkerrors.Wrap(err, "failed to simulate tx"), app.trace)
}

simRes := &sdk.SimulationResponse{
Expand All @@ -764,7 +764,7 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) abci.Res

bz, err := codec.ProtoMarshalJSON(simRes, app.interfaceRegistry)
if err != nil {
return sdkerrors.QueryResult(sdkerrors.Wrap(err, "failed to JSON encode simulation response"))
return sdkerrors.QueryResultWithDebug(sdkerrors.Wrap(err, "failed to JSON encode simulation response"), app.trace)
}

return abci.ResponseQuery{
Expand All @@ -781,34 +781,32 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) abci.Res
}

default:
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query: %s", path))
return sdkerrors.QueryResultWithDebug(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query: %s", path), app.trace)
}
}

return sdkerrors.QueryResult(
return sdkerrors.QueryResultWithDebug(
sdkerrors.Wrap(
sdkerrors.ErrUnknownRequest,
"expected second parameter to be either 'simulate' or 'version', neither was present",
),
)
), app.trace)
}

func handleQueryStore(app *BaseApp, path []string, req abci.RequestQuery) abci.ResponseQuery {
// "/store" prefix for store queries
queryable, ok := app.cms.(sdk.Queryable)
if !ok {
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "multistore doesn't support queries"))
return sdkerrors.QueryResultWithDebug(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "multistore doesn't support queries"), app.trace)
}

req.Path = "/" + strings.Join(path[1:], "/")

if req.Height <= 1 && req.Prove {
return sdkerrors.QueryResult(
return sdkerrors.QueryResultWithDebug(
sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest,
"cannot query with proof when height <= 1; please provide a valid height",
),
)
), app.trace)
}

resp := queryable.Query(req)
Expand All @@ -820,11 +818,10 @@ func handleQueryStore(app *BaseApp, path []string, req abci.RequestQuery) abci.R
func handleQueryP2P(app *BaseApp, path []string) abci.ResponseQuery {
// "/p2p" prefix for p2p queries
if len(path) < 4 {
return sdkerrors.QueryResult(
return sdkerrors.QueryResultWithDebug(
sdkerrors.Wrap(
sdkerrors.ErrUnknownRequest, "path should be p2p filter <addr|id> <parameter>",
),
)
), app.trace)
}

var resp abci.ResponseQuery
Expand All @@ -841,7 +838,7 @@ func handleQueryP2P(app *BaseApp, path []string) abci.ResponseQuery {
}

default:
resp = sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "expected second parameter to be 'filter'"))
resp = sdkerrors.QueryResultWithDebug(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "expected second parameter to be 'filter'"), app.trace)
}

return resp
Expand All @@ -854,17 +851,17 @@ func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) abci.
// The QueryRouter routes using path[1]. For example, in the path
// "custom/gov/proposal", QueryRouter routes using "gov".
if len(path) < 2 || path[1] == "" {
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "no route for custom query specified"))
return sdkerrors.QueryResultWithDebug(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "no route for custom query specified"), app.trace)
}

querier := app.queryRouter.Route(path[1])
if querier == nil {
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "no custom querier found for route %s", path[1]))
return sdkerrors.QueryResultWithDebug(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "no custom querier found for route %s", path[1]), app.trace)
}

ctx, err := app.createQueryContext(req.Height, req.Prove)
if err != nil {
return sdkerrors.QueryResult(err)
return sdkerrors.QueryResultWithDebug(err, app.trace)
}

// Passes the rest of the path as an argument to the querier.
Expand All @@ -873,7 +870,7 @@ func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) abci.
// []string{"proposal", "test"} as the path.
resBytes, err := querier(ctx, path[2:], req)
if err != nil {
res := sdkerrors.QueryResult(err)
res := sdkerrors.QueryResultWithDebug(err, app.trace)
res.Height = req.Height
return res
}
Expand Down
12 changes: 12 additions & 0 deletions types/errors/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ func QueryResult(err error) abci.ResponseQuery {
}
}

// QueryResultWithDebug returns a ResponseQuery from an error. It will try to parse ABCI
// info from the error. It will use debugErrEncoder if debug parameter is true.
// Starting from v0.46, this function will be removed, and be replaced by `QueryResult`.
func QueryResultWithDebug(err error, debug bool) abci.ResponseQuery {
space, code, log := ABCIInfo(err, debug)
return abci.ResponseQuery{
Codespace: space,
Code: code,
Log: log,
}
}

// The debugErrEncoder encodes the error with a stacktrace.
func debugErrEncoder(err error) string {
return fmt.Sprintf("%+v", err)
Expand Down

0 comments on commit 842d5b2

Please sign in to comment.