forked from ckb-js/lumos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codec): molecule friendly error message (ckb-js#403)
* feat(codec): codec human-readable eror * feat(codec): add friend error for union and option * feat(codec): add friend error revert unexpected changes * feat(codec): optimize code structure for codec pack error * feat(codec): optimize code structure for codec pack error * Update packages/codec/src/error.ts Co-authored-by: Yonghui Lin <homura.dev@gmail.com> * Update packages/codec/src/error.ts Co-authored-by: Yonghui Lin <homura.dev@gmail.com> * feat(codec): some document comment change Co-authored-by: Yonghui Lin <homura.dev@gmail.com>
- Loading branch information
Showing
7 changed files
with
373 additions
and
38 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -47,6 +47,7 @@ | |
] | ||
}, | ||
"devDependencies": { | ||
"escape-string-regexp": "^4.0.0", | ||
"js-yaml": "^4.1.0" | ||
} | ||
} |
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,56 @@ | ||
// lc for lumos codec | ||
export const CODEC_OPTIONAL_PATH = "__lc_option__"; | ||
type CodecOptionalPath = typeof CODEC_OPTIONAL_PATH; | ||
export class CodecBaseParseError extends Error { | ||
constructor(message: string, public expectedType: string) { | ||
super(message); | ||
} | ||
} | ||
|
||
const CODEC_EXECUTE_ERROR_NAME = "CodecExecuteError"; | ||
export function isCodecExecuteError( | ||
error: unknown | ||
): error is CodecExecuteError { | ||
if (!(error instanceof Error)) return false; | ||
return error.name === CODEC_EXECUTE_ERROR_NAME; | ||
} | ||
|
||
/** | ||
* This Error class can collect CodecBaseParseError, and put an human-readable error | ||
*/ | ||
export class CodecExecuteError extends Error { | ||
name = CODEC_EXECUTE_ERROR_NAME; | ||
constructor(private origin: CodecBaseParseError) { | ||
super(); | ||
} | ||
|
||
keys: (number | string | CodecOptionalPath)[] = []; | ||
|
||
public updateKey(key: number | string | symbol): void { | ||
this.keys.push(key as number | string); | ||
this.message = this.getPackErrorMessage(); | ||
} | ||
|
||
private getPackErrorMessage(): string { | ||
type CodecPath = number | string | CodecOptionalPath; | ||
|
||
const reducer = (acc: string, cur: CodecPath, index: number) => { | ||
if (cur === CODEC_OPTIONAL_PATH) { | ||
cur = index === 0 ? "?" : "?."; | ||
} else if (typeof cur === "number") { | ||
cur = `[${cur}]`; | ||
} else { | ||
cur = `.${cur}`; | ||
} | ||
return acc + cur; | ||
}; | ||
|
||
const path = this.keys.reduceRight(reducer, "input"); | ||
|
||
return `Expect type ${this.origin.expectedType} at ${path} but got error: ${ | ||
this.origin.message | ||
} | ||
${this.origin.stack?.replace(/Error:.+?\n/, "")} | ||
`; | ||
} | ||
} |
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.