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 handling troubles #288

Closed
jzethar opened this issue Dec 26, 2024 · 2 comments
Closed

Error handling troubles #288

jzethar opened this issue Dec 26, 2024 · 2 comments

Comments

@jzethar
Copy link

jzethar commented Dec 26, 2024

Problem

In TON there are shards. And knowing only address we can't find out the shard on which this contract exists. So we have to try each shard by running contract's method.

for _, s := range shards {
	if result, err = client.RunGetMethod(context.Background(), s, addr, function, params...); err != nil {
		log.Info().Msg(err.Error())
		continue // If there are a lot of shards the contract doesn't exists in a shard // HOWEVER contract could might not exist
	} else {
		break
	}
}

However, using your package it's hard to distinguish causes of errors:

{"level":"info","time":"2024-12-27T00:48:34+05:00","message":"lite server error, code -400: requested account id is not contained in the shard of the reference block"}
{"level":"info","time":"2024-12-27T00:49:50+05:00","message":"contract exit code: 11"}

Do I have to parse the string to get rc or lite server error?

Possible solution

In Go error is an interface:

// The error built-in interface type is the conventional interface for
// representing an error condition, with the nil value representing no error.
type error interface {
	Error() string
}

So it's better to create a batch of constants:

const (
ServerErr int = iota
ContractCallErr
UnpackErr
CastErr
)

And overload interface:

type TonGoError struct {
ErrorCode int
OutsideErr string
}
func (tg TonGoError) Error() string {
    return fmt.Sprintf("There is an error with rc: %d and outside error: %s"), tg.ErrorCode, tg.OutsideErr)
}

It's a simple example that can clarify all errors in you project and shall help others to handle them

@xssnick
Copy link
Owner

xssnick commented Dec 27, 2024

Hi, you can just pass master block to RunGetMethod instead of shard block, LS will map shard block automatically according to reference in master block.

If you still need to know shard, it can be calculated based on address, first bits of address hash part will indicate shard, there is a method exists for that

Regarding the execution errors, they are already wrapped to types, you can cast and check, if you still want :)

@jzethar
Copy link
Author

jzethar commented Dec 27, 2024

Thank you, everything worked!

@jzethar jzethar closed this as completed Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants