-
Notifications
You must be signed in to change notification settings - Fork 434
fix:parseFunction error: failed to handle empty string input "''" #1677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
fix:parseFunction error: failed to handle empty string input "''" #1677
Conversation
WalkthroughRemoved a non-functional "npm" utility component from three mock JSON files and hardened Changes
Sequence Diagram(s)sequenceDiagram
participant C as Caller
participant P as parseFunction
participant E as Evaluator
C->>P: call parseFunction(rawCode, context)
alt rawCode falsy or ""/''
P-->>C: return null
else
P->>E: Function('return (' + rawCode + ')').call(context)
alt evaluation throws
E-->>P: throws
P-->>C: return null
else evaluation returns result
E-->>P: result
alt result is function
P-->>C: return result.bind(context)
else
P-->>C: return null
end
end
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Points to check:
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (4)
💤 Files with no reviewable changes (3)
🚧 Files skipped from review as they are similar to previous changes (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/utils/src/utils/index.ts (1)
50-50: Consider improving error logging for better debugging.
JSON.stringify(error)typically returns"{}"for Error objects because their properties are non-enumerable, which explains the unhelpful error message mentioned in the PR description. While your defensive checks should prevent most errors from reaching this point, consider using more informative error logging.Apply this diff to improve error logging:
- console.error(`parseFunction error: ${JSON.stringify(error)}`) + console.error('parseFunction error:', error?.message || error)Or for more detail:
- console.error(`parseFunction error: ${JSON.stringify(error)}`) + console.error(`parseFunction error: ${error?.stack || error?.message || String(error)}`)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
mockServer/src/assets/json/appinfo.json(0 hunks)mockServer/src/mock/get/app-center/apps/extension/list.json(0 hunks)mockServer/src/mock/get/app-center/v1/apps/schema/1.json(0 hunks)packages/utils/src/utils/index.ts(1 hunks)
💤 Files with no reviewable changes (3)
- mockServer/src/mock/get/app-center/apps/extension/list.json
- mockServer/src/mock/get/app-center/v1/apps/schema/1.json
- mockServer/src/assets/json/appinfo.json
🔇 Additional comments (2)
packages/utils/src/utils/index.ts (2)
44-44: Good defensive check that fixes the root cause.The early return correctly handles the case described in the PR where
rawCode === "''"(a string literal containing two single quotes) would evaluate to an empty string when passed to the Function constructor, which would then fail when.bind()is called on the non-function result.Note: The
!rawCodecheck also catches all falsy values (0, false, null, undefined, empty string), which is appropriate for a code string parser.
46-47: Excellent defensive programming to prevent runtime errors.The type check prevents calling
.bind()on non-function results, which was the underlying cause of the error whenrawCodeevaluated to a string or other non-function value. This robustly handles cases where dynamic code evaluation produces unexpected types.
ddc4299 to
2105da0
Compare
| if (!rawCode || rawCode === "''" || rawCode === '""') return null | ||
| try { | ||
| return fun_ctor(`return (${rawCode})`).call(context).bind(context) | ||
| const result = fun_ctor(`return (${rawCode})`).call(context) | ||
| return typeof result === 'function' ? result.bind(context) : null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里可能不做拦截更好,直接进入 try catch,有错误可以直接控制台上看到错误,方便快速定位错误。
如果是希望不打印错误,或者是向上抛出错误,可以考虑增加配置/增加实现
页面报错parseFunction error: {},原因是parseFunction函数传入的rawCode是"''",mock中的无用数据导致的
English | 简体中文
PR
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
Background and solution
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information
Summary by CodeRabbit
Bug Fixes
Chores