-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #304 from ElementsProject/ct-discount
liquid: ct discount
- Loading branch information
Showing
12 changed files
with
246 additions
and
72 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package lwk | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
// electrumRPCError represents the structure of an RPC error response | ||
type electrumRPCError struct { | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
} | ||
|
||
// Regular expression to match RPC error messages with any prefix | ||
var re = regexp.MustCompile(`^(.*) RPC error: (.*)$`) | ||
|
||
// parseRPCError parses an error and extracts the RPC error code and message if present | ||
func parseRPCError(err error) (*electrumRPCError, error) { | ||
var rpcErr electrumRPCError | ||
errStr := err.Error() | ||
|
||
matches := re.FindStringSubmatch(errStr) | ||
|
||
if len(matches) == 3 { // Prefix and JSON payload extracted successfully | ||
errJSON := matches[2] | ||
if jerr := json.Unmarshal([]byte(errJSON), &rpcErr); jerr != nil { | ||
return nil, fmt.Errorf("error parsing rpc error: %v", jerr) | ||
} | ||
} else { | ||
// If no RPC error pattern is found, return the original error | ||
return nil, errors.New(errStr) | ||
} | ||
|
||
return &rpcErr, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package lwk | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func TestParseRPCError(t *testing.T) { | ||
t.Parallel() | ||
testCases := map[string]struct { | ||
err error | ||
expectedCode int | ||
expectedMsg string | ||
wantErr bool | ||
}{ | ||
"Valid RPC error": { | ||
err: errors.New("sendrawtransaction RPC error: {\"code\":-26,\"message\":\"min relay fee not met\"}"), | ||
expectedCode: -26, | ||
expectedMsg: "min relay fee not met", | ||
wantErr: false, | ||
}, | ||
"Invalid JSON payload": { | ||
|
||
err: errors.New("RPC error: {invalid json}"), | ||
expectedCode: 0, | ||
expectedMsg: "", | ||
wantErr: true, | ||
}, | ||
"No RPC error pattern": { | ||
err: errors.New("Some other error"), | ||
expectedCode: 0, | ||
expectedMsg: "", | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
rpcErr, err := parseRPCError(tc.err) | ||
if (err != nil) != tc.wantErr { | ||
t.Errorf("wantErr: %v, got error: %v", tc.wantErr, err) | ||
} | ||
if err == nil { | ||
if rpcErr.Code != tc.expectedCode { | ||
t.Errorf("expected code: %d, got: %d", tc.expectedCode, rpcErr.Code) | ||
} | ||
if rpcErr.Message != tc.expectedMsg { | ||
t.Errorf("expected message: %s, got: %s", tc.expectedMsg, rpcErr.Message) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.