|
| 1 | +package da |
| 2 | + |
| 3 | +import ( |
| 4 | + "google.golang.org/grpc/codes" |
| 5 | + "google.golang.org/grpc/status" |
| 6 | + |
| 7 | + pbda "github.com/rollkit/go-da/types/pb/da" |
| 8 | +) |
| 9 | + |
| 10 | +// Code defines error codes for JSON-RPC. |
| 11 | +// |
| 12 | +// They are reused for gRPC |
| 13 | +type Code int |
| 14 | + |
| 15 | +// gRPC checks for GRPCStatus method on errors to enable advanced error handling. |
| 16 | + |
| 17 | +// Codes are used by JSON-RPC client and server |
| 18 | +const ( |
| 19 | + CodeBlobNotFound Code = 32001 |
| 20 | + CodeBlobSizeOverLimit Code = 32002 |
| 21 | + CodeTxTimedOut Code = 32003 |
| 22 | + CodeTxAlreadyInMempool Code = 32004 |
| 23 | + CodeTxIncorrectAccountSequence Code = 32005 |
| 24 | + CodeTxTooLarge Code = 32006 |
| 25 | + CodeContextDeadline Code = 32007 |
| 26 | + CodeFutureHeight Code = 32008 |
| 27 | +) |
| 28 | + |
| 29 | +// ErrBlobNotFound is used to indicate that the blob was not found. |
| 30 | +type ErrBlobNotFound struct{} |
| 31 | + |
| 32 | +func (e *ErrBlobNotFound) Error() string { |
| 33 | + return "blob: not found" |
| 34 | +} |
| 35 | + |
| 36 | +// GRPCStatus returns the gRPC status with details for an ErrBlobNotFound error. |
| 37 | +func (e *ErrBlobNotFound) GRPCStatus() *status.Status { |
| 38 | + return getGRPCStatus(e, codes.NotFound, pbda.ErrorCode_ERROR_CODE_BLOB_NOT_FOUND) |
| 39 | +} |
| 40 | + |
| 41 | +// ErrBlobSizeOverLimit is used to indicate that the blob size is over limit. |
| 42 | +type ErrBlobSizeOverLimit struct{} |
| 43 | + |
| 44 | +func (e *ErrBlobSizeOverLimit) Error() string { |
| 45 | + return "blob: over size limit" |
| 46 | +} |
| 47 | + |
| 48 | +// GRPCStatus returns the gRPC status with details for an ErrBlobSizeOverLimit error. |
| 49 | +func (e *ErrBlobSizeOverLimit) GRPCStatus() *status.Status { |
| 50 | + return getGRPCStatus(e, codes.ResourceExhausted, pbda.ErrorCode_ERROR_CODE_BLOB_SIZE_OVER_LIMIT) |
| 51 | +} |
| 52 | + |
| 53 | +// ErrTxTimedOut is the error message returned by the DA when mempool is congested. |
| 54 | +type ErrTxTimedOut struct{} |
| 55 | + |
| 56 | +func (e *ErrTxTimedOut) Error() string { |
| 57 | + return "timed out waiting for tx to be included in a block" |
| 58 | +} |
| 59 | + |
| 60 | +// GRPCStatus returns the gRPC status with details for an ErrTxTimedOut error. |
| 61 | +func (e *ErrTxTimedOut) GRPCStatus() *status.Status { |
| 62 | + return getGRPCStatus(e, codes.DeadlineExceeded, pbda.ErrorCode_ERROR_CODE_TX_TIMED_OUT) |
| 63 | +} |
| 64 | + |
| 65 | +// ErrTxAlreadyInMempool is the error message returned by the DA when tx is already in mempool. |
| 66 | +type ErrTxAlreadyInMempool struct{} |
| 67 | + |
| 68 | +func (e *ErrTxAlreadyInMempool) Error() string { |
| 69 | + return "tx already in mempool" |
| 70 | +} |
| 71 | + |
| 72 | +// GRPCStatus returns the gRPC status with details for an ErrTxAlreadyInMempool error. |
| 73 | +func (e *ErrTxAlreadyInMempool) GRPCStatus() *status.Status { |
| 74 | + return getGRPCStatus(e, codes.AlreadyExists, pbda.ErrorCode_ERROR_CODE_TX_ALREADY_IN_MEMPOOL) |
| 75 | +} |
| 76 | + |
| 77 | +// ErrTxIncorrectAccountSequence is the error message returned by the DA when tx has incorrect sequence. |
| 78 | +type ErrTxIncorrectAccountSequence struct{} |
| 79 | + |
| 80 | +func (e *ErrTxIncorrectAccountSequence) Error() string { |
| 81 | + return "incorrect account sequence" |
| 82 | +} |
| 83 | + |
| 84 | +// GRPCStatus returns the gRPC status with details for an ErrTxIncorrectAccountSequence error. |
| 85 | +func (e *ErrTxIncorrectAccountSequence) GRPCStatus() *status.Status { |
| 86 | + return getGRPCStatus(e, codes.InvalidArgument, pbda.ErrorCode_ERROR_CODE_TX_INCORRECT_ACCOUNT_SEQUENCE) |
| 87 | +} |
| 88 | + |
| 89 | +// ErrTxTooLarge is the err message returned by the DA when tx size is too large. |
| 90 | +type ErrTxTooLarge struct{} |
| 91 | + |
| 92 | +func (e *ErrTxTooLarge) Error() string { |
| 93 | + return "tx too large" |
| 94 | +} |
| 95 | + |
| 96 | +// GRPCStatus returns the gRPC status with details for an ErrTxTooLarge error. |
| 97 | +func (e *ErrTxTooLarge) GRPCStatus() *status.Status { |
| 98 | + return getGRPCStatus(e, codes.ResourceExhausted, pbda.ErrorCode_ERROR_CODE_TX_TOO_LARGE) |
| 99 | +} |
| 100 | + |
| 101 | +// ErrContextDeadline is the error message returned by the DA when context deadline exceeds. |
| 102 | +type ErrContextDeadline struct{} |
| 103 | + |
| 104 | +func (e *ErrContextDeadline) Error() string { |
| 105 | + return "context deadline" |
| 106 | +} |
| 107 | + |
| 108 | +// GRPCStatus returns the gRPC status with details for an ErrContextDeadline error. |
| 109 | +func (e *ErrContextDeadline) GRPCStatus() *status.Status { |
| 110 | + return getGRPCStatus(e, codes.DeadlineExceeded, pbda.ErrorCode_ERROR_CODE_CONTEXT_DEADLINE) |
| 111 | +} |
| 112 | + |
| 113 | +// ErrFutureHeight is returned when requested height is from the future |
| 114 | +type ErrFutureHeight struct{} |
| 115 | + |
| 116 | +func (e *ErrFutureHeight) Error() string { |
| 117 | + return "given height is from the future" |
| 118 | +} |
| 119 | + |
| 120 | +// GRPCStatus returns the gRPC status with details for an ErrFutureHeight error. |
| 121 | +func (e *ErrFutureHeight) GRPCStatus() *status.Status { |
| 122 | + return getGRPCStatus(e, codes.OutOfRange, pbda.ErrorCode_ERROR_CODE_FUTURE_HEIGHT) |
| 123 | +} |
| 124 | + |
| 125 | +// getGRPCStatus constructs a gRPC status with error details based on the provided error, gRPC code, and DA error code. |
| 126 | +func getGRPCStatus(err error, grpcCode codes.Code, daCode pbda.ErrorCode) *status.Status { |
| 127 | + base := status.New(grpcCode, err.Error()) |
| 128 | + detailed, err := base.WithDetails(&pbda.ErrorDetails{Code: daCode}) |
| 129 | + if err != nil { |
| 130 | + return base |
| 131 | + } |
| 132 | + return detailed |
| 133 | +} |
0 commit comments