-
Notifications
You must be signed in to change notification settings - Fork 21
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
feat: implement custom error types for DA #115
Open
tzdybal
wants to merge
9
commits into
main
Choose a base branch
from
tzdybal/errors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1ffb634
feat: implement custom error types for DA
tzdybal 31031bb
feat: add detailed gRPC error handling for DA errors
tzdybal 25a3a03
refactor: add proper support for future height error
tzdybal 15443a3
feat: add error mapping to all methods in gRPC proxy client
tzdybal e875df4
refactor: extract error mapping into a reusable function
tzdybal e0bdd5c
refactor: reformat ErrorCode enum entries
tzdybal 181b674
chore: fix protobuf enum names (linting)
tzdybal e9f4c7a
chore: regenerate protobuf with latest version of protoc-gen-gocosmos
tzdybal 61b9787
docs: fix typos in comments
tzdybal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package da | ||
|
||
import ( | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
pbda "github.com/rollkit/go-da/types/pb/da" | ||
) | ||
|
||
// Code defines error codes for JSON-RPC. | ||
// | ||
// They are reused for gRPC | ||
type Code int | ||
|
||
// Codes are used by JSON-RPC client and server | ||
const ( | ||
CodeBlobNotFound Code = 32001 | ||
CodeBlobSizeOverLimit Code = 32002 | ||
CodeTxTimedOut Code = 32003 | ||
CodeTxAlreadyInMempool Code = 32004 | ||
CodeTxIncorrectAccountSequence Code = 32005 | ||
CodeTxTooLarge Code = 32006 | ||
CodeContextDeadline Code = 32007 | ||
CodeFutureHeight Code = 32008 | ||
) | ||
|
||
// ErrBlobNotFound is used to indicate that the blob was not found. | ||
type ErrBlobNotFound struct{} | ||
|
||
func (e *ErrBlobNotFound) Error() string { | ||
return "blob: not found" | ||
} | ||
|
||
// ErrBlobSizeOverLimit is used to indicate that the blob size is over limit. | ||
type ErrBlobSizeOverLimit struct{} | ||
|
||
func (e *ErrBlobSizeOverLimit) Error() string { | ||
return "blob: over size limit" | ||
} | ||
|
||
// ErrTxTimedOut is the error message returned by the DA when mempool is congested. | ||
type ErrTxTimedOut struct{} | ||
|
||
func (e *ErrTxTimedOut) Error() string { | ||
return "timed out waiting for tx to be included in a block" | ||
} | ||
|
||
// ErrTxAlreadyInMempool is the error message returned by the DA when tx is already in mempool. | ||
type ErrTxAlreadyInMempool struct{} | ||
|
||
func (e *ErrTxAlreadyInMempool) Error() string { | ||
return "tx already in mempool" | ||
} | ||
|
||
// ErrTxIncorrectAccountSequence is the error message returned by the DA when tx has incorrect sequence. | ||
type ErrTxIncorrectAccountSequence struct{} | ||
|
||
func (e *ErrTxIncorrectAccountSequence) Error() string { | ||
return "incorrect account sequence" | ||
} | ||
|
||
// ErrTxTooLarge is the err message returned by the DA when tx size is too large. | ||
type ErrTxTooLarge struct{} | ||
|
||
func (e *ErrTxTooLarge) Error() string { | ||
return "tx too large" | ||
} | ||
|
||
// ErrContextDeadline is the error message returned by the DA when context deadline exceeds. | ||
type ErrContextDeadline struct{} | ||
|
||
func (e *ErrContextDeadline) Error() string { | ||
return "context deadline" | ||
} | ||
|
||
// ErrFutureHeight is returned when requested height is from the future | ||
type ErrFutureHeight struct{} | ||
|
||
func (e *ErrFutureHeight) Error() string { | ||
return "given height is from the future" | ||
} | ||
|
||
// gRPC checks for GRPCStatus method on errors to enable advanced error handling. | ||
|
||
// getGRPCStatus constructs a gRPC status with error details based on the provided error, gRPC code, and DA error code. | ||
func getGRPCStatus(err error, grpcCode codes.Code, daCode pbda.ErrorCode) *status.Status { | ||
base := status.New(grpcCode, err.Error()) | ||
detailed, err := base.WithDetails(&pbda.ErrorDetails{Code: daCode}) | ||
if err != nil { | ||
return base | ||
} | ||
return detailed | ||
} | ||
|
||
// GRPCStatus returns the gRPC status with details for an ErrBlobNotFound error. | ||
func (e *ErrBlobNotFound) GRPCStatus() *status.Status { | ||
return getGRPCStatus(e, codes.NotFound, pbda.ErrorCode_ERROR_CODE_BLOB_NOT_FOUND) | ||
} | ||
|
||
// GRPCStatus returns the gRPC status with details for an ErrBlobSizeOverLimit error. | ||
func (e *ErrBlobSizeOverLimit) GRPCStatus() *status.Status { | ||
return getGRPCStatus(e, codes.ResourceExhausted, pbda.ErrorCode_ERROR_CODE_BLOB_SIZE_OVER_LIMIT) | ||
} | ||
|
||
// GRPCStatus returns the gRPC status with details for an ErrTxTimedOut error. | ||
func (e *ErrTxTimedOut) GRPCStatus() *status.Status { | ||
return getGRPCStatus(e, codes.DeadlineExceeded, pbda.ErrorCode_ERROR_CODE_TX_TIMED_OUT) | ||
} | ||
|
||
// GRPCStatus returns the gRPC status with details for an ErrTxAlreadyInMempool error. | ||
func (e *ErrTxAlreadyInMempool) GRPCStatus() *status.Status { | ||
return getGRPCStatus(e, codes.AlreadyExists, pbda.ErrorCode_ERROR_CODE_TX_ALREADY_IN_MEMPOOL) | ||
} | ||
|
||
// GRPCStatus returns the gRPC status with details for an ErrTxIncorrectAccountSequence error. | ||
func (e *ErrTxIncorrectAccountSequence) GRPCStatus() *status.Status { | ||
return getGRPCStatus(e, codes.InvalidArgument, pbda.ErrorCode_ERROR_CODE_TX_INCORRECT_ACCOUNT_SEQUENCE) | ||
} | ||
|
||
// GRPCStatus returns the gRPC status with details for an ErrTxTooLarge error. | ||
func (e *ErrTxTooLarge) GRPCStatus() *status.Status { | ||
return getGRPCStatus(e, codes.ResourceExhausted, pbda.ErrorCode_ERROR_CODE_TX_TOO_LARGE) | ||
} | ||
|
||
// GRPCStatus returns the gRPC status with details for an ErrContextDeadline error. | ||
func (e *ErrContextDeadline) GRPCStatus() *status.Status { | ||
return getGRPCStatus(e, codes.DeadlineExceeded, pbda.ErrorCode_ERROR_CODE_CONTEXT_DEADLINE) | ||
} | ||
|
||
// GRPCStatus returns the gRPC status with details for an ErrFutureHeight error. | ||
func (e *ErrFutureHeight) GRPCStatus() *status.Status { | ||
return getGRPCStatus(e, codes.OutOfRange, pbda.ErrorCode_ERROR_CODE_FUTURE_HEIGHT) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package grpc | ||
|
||
import ( | ||
"errors" | ||
|
||
"google.golang.org/grpc/status" | ||
|
||
"github.com/rollkit/go-da" | ||
pbda "github.com/rollkit/go-da/types/pb/da" | ||
) | ||
|
||
func tryToMapError(err error) error { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
s, ok := status.FromError(err) | ||
if ok { | ||
details := s.Proto().Details | ||
if len(details) == 1 { | ||
var errorDetail pbda.ErrorDetails | ||
unmarshalError := errorDetail.Unmarshal(details[0].Value) | ||
if unmarshalError != nil { | ||
return err | ||
} | ||
tzdybal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return errorForCode(errorDetail.Code) | ||
} | ||
} | ||
return err | ||
} | ||
tzdybal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func errorForCode(code pbda.ErrorCode) error { | ||
switch code { | ||
case pbda.ErrorCode_ERROR_CODE_BLOB_NOT_FOUND: | ||
return &da.ErrBlobNotFound{} | ||
case pbda.ErrorCode_ERROR_CODE_BLOB_SIZE_OVER_LIMIT: | ||
return &da.ErrBlobSizeOverLimit{} | ||
case pbda.ErrorCode_ERROR_CODE_TX_TIMED_OUT: | ||
return &da.ErrTxTimedOut{} | ||
case pbda.ErrorCode_ERROR_CODE_TX_ALREADY_IN_MEMPOOL: | ||
return &da.ErrTxAlreadyInMempool{} | ||
case pbda.ErrorCode_ERROR_CODE_TX_INCORRECT_ACCOUNT_SEQUENCE: | ||
return &da.ErrTxIncorrectAccountSequence{} | ||
case pbda.ErrorCode_ERROR_CODE_TX_TOO_LARGE: | ||
return &da.ErrTxTooLarge{} | ||
case pbda.ErrorCode_ERROR_CODE_CONTEXT_DEADLINE: | ||
return &da.ErrContextDeadline{} | ||
case pbda.ErrorCode_ERROR_CODE_FUTURE_HEIGHT: | ||
return &da.ErrFutureHeight{} | ||
default: | ||
return errors.New("unknown error code") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package jsonrpc | ||
|
||
import ( | ||
"github.com/filecoin-project/go-jsonrpc" | ||
|
||
"github.com/rollkit/go-da" | ||
) | ||
|
||
// getKnownErrorsMapping returns a mapping of known error codes to their corresponding error types. | ||
func getKnownErrorsMapping() jsonrpc.Errors { | ||
errs := jsonrpc.NewErrors() | ||
errs.Register(jsonrpc.ErrorCode(da.CodeBlobNotFound), new(*da.ErrBlobNotFound)) | ||
errs.Register(jsonrpc.ErrorCode(da.CodeBlobSizeOverLimit), new(*da.ErrBlobSizeOverLimit)) | ||
errs.Register(jsonrpc.ErrorCode(da.CodeTxTimedOut), new(*da.ErrTxTimedOut)) | ||
errs.Register(jsonrpc.ErrorCode(da.CodeTxAlreadyInMempool), new(*da.ErrTxAlreadyInMempool)) | ||
errs.Register(jsonrpc.ErrorCode(da.CodeTxIncorrectAccountSequence), new(*da.ErrTxIncorrectAccountSequence)) | ||
errs.Register(jsonrpc.ErrorCode(da.CodeTxTooLarge), new(*da.ErrTxTooLarge)) | ||
errs.Register(jsonrpc.ErrorCode(da.CodeContextDeadline), new(*da.ErrContextDeadline)) | ||
errs.Register(jsonrpc.ErrorCode(da.CodeFutureHeight), new(*da.ErrFutureHeight)) | ||
return errs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when does this happen? doesn't look like it is being used anywhere?