import ( "fmt" "github.com/cockroachdb/cockroach/pkg/roachpb" ) func myFunc() error { customErr := roachpb.NewErrorf(roachpb.SOMETHING_IS_WRONG, "Something went wrong") return customErr.GoError() }
import ( "fmt" "github.com/cockroachdb/cockroach/pkg/roachpb" "github.com/cockroachdb/cockroach/pkg/sql" ) func myFunc() error { _, err := sql.SomethingThatReturnsAnError() if err != nil { if pgErr, ok := err.(*roachpb.Error); ok { if pgErr.Code == roachpb.SOMETHING_IS_WRONG { fmt.Println("Something went wrong, please try again later") } else { fmt.Println("Unknown error occurred") } return pgErr.GoError() } fmt.Println("Unknown error occurred") return err } return nil }In this example, we handle an error returned by a CockroachDB SQL function. We check if the error is of type `*roachpb.Error` and then determine the error code to provide a specific error message. If the error is not of type `*roachpb.Error`, we print an "Unknown error occurred" message. Overall, the roachpb package is used to define error codes and messages used by CockroachDB across different layers of the stack.