Skip to content

Commit

Permalink
fix lint & add error handling for json parse
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyuki committed Jun 21, 2024
1 parent 5e2740e commit 457aa05
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('test create-selector-query', () => {
template: `
<div>
<div class="class1">child class1</div>
<div class="class2" data-re="class2" style="background-color:red">child class2</div>
<div class="class2" data-re="class2" style="background-color:red">child class2</div>
<div class="class2" data-re='"class2"' style="background-color:red">child class2</div>
<div class="class2" data-re='"class2"' style="background-color:red">child class2</div>
</div>
`,
mounted () {
Expand Down
7 changes: 6 additions & 1 deletion packages/api-proxy/src/common/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function warn (msg) {
function error (msg) {
console.error && console.error(`[@mpxjs/api-proxy error]:\n ${msg}`)
}

function envError (method) {
return () => {
console.error && console.error(`[@mpxjs/api-proxy error]:\n ${__mpx_mode__}环境不支持${method}方法`)
Expand All @@ -99,7 +100,11 @@ function parseDataset (dataset) {
const parsed = {}
for (const key in dataset) {
if (hasOwn(dataset, key)) {
parsed[key] = JSON.parse(dataset[key])
try {
parsed[key] = JSON.parse(dataset[key])
} catch (e) {
parsed[key] = dataset[key]
}
}
}
return parsed
Expand Down
10 changes: 9 additions & 1 deletion packages/utils/src/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ function collectDataset (props, needParse = false) {
if (hasOwn(props, key)) {
const matched = datasetReg.exec(key)
if (matched) {
dataset[matched[1]] = needParse ? JSON.parse(props[key]) : props[key]
if (needParse) {
try {
dataset[matched[1]] = JSON.parse(props[key])
} catch (e) {
dataset[matched[1]] = props[key]
}
} else {
dataset[matched[1]] = props[key]
}
}
}
}
Expand Down

0 comments on commit 457aa05

Please sign in to comment.