Skip to content

Commit

Permalink
fix: correct error message for computeScriptHash (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
twhy authored May 17, 2024
1 parent 22a6fc2 commit d483b92
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/silent-mangos-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ckb-lumos/codec": patch
"@ckb-lumos/base": patch
---

fix: correct error message for computeScriptHash
8 changes: 8 additions & 0 deletions packages/base/tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ const scriptHash =

test("computeScriptHash", (t) => {
t.is(computeScriptHash(script), scriptHash);
t.throws(
() =>
computeScriptHash({ codeHash: "000", hashType: "type", args: "0x12" }),
{
instanceOf: Error,
message: new RegExp("Error at input.codeHash.*", "m"),
}
);
});

test("hashCode, should return same hash if same input", (t) => {
Expand Down
8 changes: 5 additions & 3 deletions packages/codec/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ export class CodecExecuteError extends Error {
};

const path = this.keys.reduceRight(reducer, "input");
const text =
this.origin instanceof CodecBaseParseError
? `Expect type ${this.origin.expectedType} at ${path} but got error:`
: `Error at ${path}:`; // this.origin can be an Error at runtime

return `Expect type ${this.origin.expectedType} at ${path} but got error: ${
this.origin.message
}
return `${text} ${this.origin.message}
${this.origin.stack?.replace(/Error:.+?\n/, "")}
`;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/codec/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const CHAR_f = "f".charCodeAt(0); // 102

function assertStartsWith0x(str: string): void {
if (!str || !str.startsWith("0x")) {
throw new Error("Invalid hex string");
throw new Error("Invalid hex string, expect starts with 0x");
}
}

Expand Down Expand Up @@ -79,6 +79,7 @@ export function assertHexString(str: string, byteLength?: number): void {
export function assertUtf8String(str: string): void {
for (let i = 0; i < str.length; i++) {
const c = str.charCodeAt(i);
/* eslint-disable @typescript-eslint/no-magic-numbers */
if (c > 0xff) {
throw new Error("Invalid UTF-8 raw string!");
}
Expand Down

1 comment on commit d483b92

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 New canary release: 0.0.0-canary-d483b92-20240517031918

npm install @ckb-lumos/[email protected]

Please sign in to comment.