Skip to content

Commit

Permalink
Fix error details handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix authored and bchamagne committed Aug 13, 2024
1 parent 1ada273 commit bf39455
Showing 1 changed file with 21 additions and 37 deletions.
58 changes: 21 additions & 37 deletions tui/tuiutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ func CreateKeychain(url string, accessSeed []byte) (string, string, string, stri
ts2.Unsubscribe("confirmation")
keychainAccessTransactionAddress = fmt.Sprintf("%s/explorer/transaction/%x", url, accessAddress)
})
ts2.AddOnError(func(senderContext string, error archethic.ErrorDetails) {
feedback += handleTransactionError(error).Error()
ts2.AddOnError(func(context string, error archethic.ErrorDetails) {
feedback += handleTransactionError(context, error).Error()
ts.Unsubscribe("error")
})
ts2.SendTransaction(accessTx, 100, 60)
ts.Unsubscribe("confirmation")
})
ts.AddOnError(func(senderContext string, error archethic.ErrorDetails) {
returnedError = handleTransactionError(error)
ts.AddOnError(func(context string, error archethic.ErrorDetails) {
returnedError = handleTransactionError(context, error)
feedback += returnedError.Error()
ts.Unsubscribe("error")
})
Expand Down Expand Up @@ -172,15 +172,15 @@ func updateKeychain(accessSeed []byte, endpoint string, updateFunc func(*archeth
transaction.OriginSign(originPrivateKey)

var returnedError error
var returnedFeedback = ""
returnedFeedback := ""
returnedError = nil

ts := archethic.NewTransactionSender(&client)
ts.AddOnRequiredConfirmation(func(nbConf int) {
returnedFeedback = "\nKeychain's transaction confirmed."
})
ts.AddOnError(func(senderContext string, error archethic.ErrorDetails) {
returnedError = handleTransactionError(error)
ts.AddOnError(func(context string, error archethic.ErrorDetails) {
returnedError = handleTransactionError(context, error)
ts.Unsubscribe("error")
})
ts.SendTransaction(transaction, 100, 60)
Expand All @@ -200,8 +200,8 @@ func SendTransaction(transaction *archethic.TransactionBuilder, secretKey []byte
feedback = endpoint + "/explorer/transaction/" + strings.ToUpper(hex.EncodeToString(transaction.Address))
})

ts.AddOnError(func(sender string, error archethic.ErrorDetails) {
feedback = handleTransactionError(error).Error()
ts.AddOnError(func(context string, error archethic.ErrorDetails) {
feedback = handleTransactionError(context, error).Error()
})

ts.SendTransaction(transaction, 100, 60)
Expand All @@ -228,7 +228,7 @@ func GetTransactionFee(transaction *archethic.TransactionBuilder, secretKey []by
client := archethic.NewAPIClient(endpoint)
fee, err := client.GetTransactionFee(transaction)
if err != nil {
return archethic.Fee{}, handleTransactionError(err)
return archethic.Fee{}, handleTransactionError("INVALID_TRANSACTION", err)
}
return fee, nil
}
Expand Down Expand Up @@ -336,7 +336,6 @@ func GetSSHPrivateKey(privateKeyPath string) ([]byte, error) {
return nil, errors.New("Only RSA, ECDSA and DSA keys are supported, got " + reflect.TypeOf(pvKey).String())
}
return pvKeyBytes, nil

}

func promptSecret(message string) string {
Expand Down Expand Up @@ -448,45 +447,30 @@ type customError struct {
Data any `yaml:"Data,omitempty"`
}

func customErrorDetails(error archethic.ErrorDetails) string {
func customErrorDetails(context string, error archethic.ErrorDetails) string {
var message string

var throwData archethic.ErrorDetails
var customErr customError
message = fmt.Sprintf("%s (%d)", error.Message, error.Code)

errDataBytes, err := json.Marshal(error.Data)
if err != nil {
return err.Error()
customErr = customError{
Error: context,
Message: message,
}
if err := json.Unmarshal(errDataBytes, &throwData); err != nil {
return err.Error()
}

var customErr customError
if throwData.Message == "" {
customErr = customError{
Error: fmt.Sprintf("%s (%d)", error.Message, error.Code),
}
} else {
message = fmt.Sprintf("%s (%d)", throwData.Message, throwData.Code)

customErr = customError{
Error: fmt.Sprintf("%s (%d)", error.Message, error.Code),
Message: message,
}
if message != "" && throwData.Data != nil {
customDataDetails, _ := yaml.Marshal(throwData.Data)
customErr.Data = fmt.Sprintf("%s", customDataDetails)
}
if error.Data != nil {
customDataDetails, _ := yaml.Marshal(error.Data)
customErr.Data = fmt.Sprintf("%s", customDataDetails)
}

b, _ := yaml.Marshal(customErr)

return fmt.Sprintf("\n%s", b)
}

func handleTransactionError(err error) error {
func handleTransactionError(context string, err error) error {
if errorDetails, ok := err.(archethic.ErrorDetails); ok {
return errors.New(customErrorDetails(errorDetails))
return errors.New(customErrorDetails(context, errorDetails))
}

if jsonRpcError, ok := err.(*jsonrpc.RPCError); ok {
Expand Down

0 comments on commit bf39455

Please sign in to comment.