status-go/errors/errors.go
Sale Djenic 9b9a91f654 chore_: map insufficient funds error
This commit maps this kind of errors
`status-proxy-0.error: failed with 50011064 gas: insufficient funds for gas * price + value: address 0x4eeB09cf0076F840b38511D808464eE48efD4305 have 0 want 10000000000000`
to this form:
`status-proxy-0.error: failed with 50011064 gas: insufficient funds for gas * price + value: address 0x4eeB09cf0076F840b38511D808464eE48efD4305`

which means that we don't want to display to a user details of how much they have and how much is needed for fees, cause those data are very often
misleading, referring mostly to "how much user has".

New error added in case there is no positive balances across all enabled chains.
2024-08-28 08:23:18 +02:00

65 lines
1.4 KiB
Go

package errors
import (
"encoding/json"
)
// ErrorCode represents a specific error code.
type ErrorCode string
const GenericErrorCode ErrorCode = "0"
// ErrorResponse represents an error response structure.
type ErrorResponse struct {
Code ErrorCode `json:"code"`
Details string `json:"details,omitempty"`
}
// Error implements the error interface for ErrorResponse.
func (e *ErrorResponse) Error() string {
errorJSON, _ := json.Marshal(e)
return string(errorJSON)
}
// IsErrorResponse determines if an error is an ErrorResponse.
func IsErrorResponse(err error) bool {
_, ok := err.(*ErrorResponse)
return ok
}
// ErrorCodeFromError returns the ErrorCode from an error.
func ErrorCodeFromError(err error) ErrorCode {
if err == nil {
return GenericErrorCode
}
if errResp, ok := err.(*ErrorResponse); ok {
return errResp.Code
}
return GenericErrorCode
}
// DetailsFromError returns the details from an error.
func DetailsFromError(err error) string {
if err == nil {
return ""
}
if errResp, ok := err.(*ErrorResponse); ok {
return errResp.Details
}
return err.Error()
}
// CreateErrorResponseFromError creates an ErrorResponse from a generic error.
func CreateErrorResponseFromError(err error) error {
if err == nil {
return nil
}
if errResp, ok := err.(*ErrorResponse); ok {
return errResp
}
return &ErrorResponse{
Code: GenericErrorCode,
Details: err.Error(),
}
}