Skip to content
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

error parsing: add borsh io error #234

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions solana-errors/from-json-to-protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,46 @@ func FromJSONToProtobuf(j map[string]interface{}) ([]byte, error) {
if firstKey == "" {
return nil, fmt.Errorf("no keys found in map")
}
if firstKey != "Custom" {
return nil, fmt.Errorf("expected a Custom key")
if firstKey != "Custom" && firstKey != "BorshIoError" {
return nil, fmt.Errorf("expected a Custom or BorshIoError key, got %q", firstKey)
}
doer.Do("write customErrorType", func() error {
return wr.WriteUint32(uint32(InstructionErrorType_CUSTOM), bin.LE)
})
customErrorTypeFloat, ok := as[firstKey].(float64)
if !ok {
return nil, fmt.Errorf("expected a float64")
switch firstKey {
case "Custom":
{
doer.Do("write customErrorType", func() error {
return wr.WriteUint32(uint32(InstructionErrorType_CUSTOM), bin.LE)
})
customErrorTypeFloat, ok := as[firstKey].(float64)
if !ok {
return nil, fmt.Errorf("expected a float64, got %T", as[firstKey])
}
customErrorType := uint32(customErrorTypeFloat)
doer.Do("write customErrorType", func() error {
return wr.WriteUint32(customErrorType, bin.LE)
})
}
case "BorshIoError":
{
doer.Do("write borshIoErrorType", func() error {
return wr.WriteUint32(uint32(InstructionErrorType_BORSH_IO_ERROR), bin.LE)
})
// BorshIoError(String),
borshIoError, ok := as[firstKey].(string)
if !ok {
return nil, fmt.Errorf("expected a string, got %T", as[firstKey])
}
doer.Do("write borshIoError", func() error {
return wr.WriteString(borshIoError)
})
}
default:
return nil, fmt.Errorf("unhandled type %T", as)
}
customErrorType := uint32(customErrorTypeFloat)
doer.Do("write customErrorType", func() error {
return wr.WriteUint32(customErrorType, bin.LE)
})
}
default:
return nil, fmt.Errorf("unhandled type %T", arr[1])
}
}

}

err := doer.Err()
Expand Down
57 changes: 53 additions & 4 deletions solana-errors/from-json-to-protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func TestFromJSONToProtobuf(t *testing.T) {
require.NotNil(t, buf)
require.Equal(t,
[]byte{
0x8, 0x0, 0x0, 0x0,
0x2,
0x19, 0x0, 0x0, 0x0,
0x71, 0x17, 0x0, 0x0,
0x8, 0x0, 0x0, 0x0, // instruction error
0x2, // error code
0x19, 0x0, 0x0, 0x0, // instruction error type
0x71, 0x17, 0x0, 0x0, // 6001
},
buf,
)
Expand All @@ -51,6 +51,55 @@ func TestFromJSONToProtobuf(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, got)

require.JSONEq(t,
toJson(t, candidate),
toJson(t, got),
)
}
}
{
candidate := map[string]any{
"InstructionError": []any{
0.0,
map[string]any{
"BorshIoError": "Unknown",
},
},
}
buf, err := FromJSONToProtobuf(
candidate,
)
require.NoError(t, err)
require.NotNil(t, buf)
require.Equal(t,
[]byte{
0x8, 0x0, 0x0, 0x0,
0x0,
0x2c, 0x0, 0x0, 0x0,
0x7, 0x55, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, // "Unknown"
},
buf,
)
require.Equal(t,
concat(
uint32tobytes(uint32(TransactionErrorType_INSTRUCTION_ERROR)),
[]byte{0x0},
uint32tobytes(uint32(InstructionErrorType_BORSH_IO_ERROR)),
// length of "Unknown"
[]byte{0x7},
[]byte("Unknown"),
),
buf,
)
{
candidateAsBase64 := base64.StdEncoding.EncodeToString(buf)
wrapped := map[string]any{
"err": candidateAsBase64,
}
got, err := ParseTransactionError(wrapped)
require.NoError(t, err)
require.NotNil(t, got)

require.JSONEq(t,
toJson(t, candidate),
toJson(t, got),
Expand Down
15 changes: 15 additions & 0 deletions solana-errors/solana-errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,21 @@ func ParseTransactionError(v any) (map[string]any, error) {
},
},
}, nil
case InstructionErrorType_BORSH_IO_ERROR:
borshIoError, err := dec.ReadString()
if err != nil {
return nil, err
}
return map[string]any{
transactionErrorTypeName: []any{
errorCode,
map[string]any{
instructionErrorTypeName: borshIoError,
},
},
}, nil
default:
return nil, fmt.Errorf("unknown instruction error type: %d", instructionErrorType)
}

return map[string]any{
Expand Down
Loading