Skip to content

Commit

Permalink
Change the default behavior of error handling to open a basic alert i…
Browse files Browse the repository at this point in the history
…nstead of logging the error in the console (see #1364).
  • Loading branch information
josdejong committed Aug 25, 2021
1 parent 883a0c9 commit cbb95ae
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 27 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Expand Up @@ -5,6 +5,8 @@ https://github.com/josdejong/jsoneditor

## not yet published, version 9.5.4

- Change the default behavior of error handling to open a basic alert instead
of logging the error in the console (see #1364).
- Use `noreferrer` for window.open. Thanks @rajitbanerjee.
- Fix #1363: parsing error contains html caharacters.

Expand Down
2 changes: 1 addition & 1 deletion docs/api.md
Expand Up @@ -119,7 +119,7 @@ Constructs a new JSONEditor.
- `{function} onError(error)`

Set a callback function triggered when an error occurs. Invoked with the error as first argument. The callback is only invoked
for errors triggered by a users action, like switching from code mode to tree mode or clicking the Format button whilst the editor doesn't contain valid JSON.
for errors triggered by a users action, like switching from code mode to tree mode or clicking the Format button whilst the editor doesn't contain valid JSON. When not defined, a basic alert with the error message will be opened.

- `{function} onModeChange(newMode, oldMode)`

Expand Down
3 changes: 0 additions & 3 deletions examples/03_switch_mode.html
Expand Up @@ -43,9 +43,6 @@
const options = {
mode: 'tree',
modes: ['code', 'form', 'text', 'tree', 'view', 'preview'], // allowed modes
onError: function (err) {
alert(err.toString())
},
onModeChange: function (newMode, oldMode) {
console.log('Mode switched from', oldMode, 'to', newMode)
}
Expand Down
3 changes: 0 additions & 3 deletions examples/09_readonly_text_mode.html
Expand Up @@ -50,9 +50,6 @@
return false;
}
},
onError: function (err) {
alert(err.toString())
},
onModeChange: function (newMode, oldMode) {
console.log('Mode switched from', oldMode, 'to', newMode)
}
Expand Down
5 changes: 1 addition & 4 deletions examples/15_selection_api.html
Expand Up @@ -21,7 +21,7 @@

code.multiline {
display: block;
white-space: pre-wrap
white-space: pre-wrap;
}

#jsoneditor {
Expand Down Expand Up @@ -63,9 +63,6 @@
const options = {
mode: 'tree',
modes: ['code', 'form', 'text', 'tree', 'view', 'preview'], // allowed modes
onError: function (err) {
alert(err.toString())
},
onChange: function () {
console.log('change')
},
Expand Down
7 changes: 2 additions & 5 deletions examples/17_on_event_api.html
Expand Up @@ -28,7 +28,7 @@
<body>

<p>
When clicking on a JSON field or value, a log message will be shown in
When clicking on a JSON field or value, a log message will be shown in
console.
</p>

Expand All @@ -43,9 +43,6 @@
mode: 'tree',
modes: ['code', 'form', 'text', 'tree', 'view', 'preview'], // allowed modes
name: "jsonContent",
onError: function (err) {
alert(err.toString())
},
onEvent: function(node, event) {
if (node.value !== undefined) {
console.log(event.type + ' event ' +
Expand Down Expand Up @@ -75,7 +72,7 @@
"url": "http://jsoneditoronline.org",
"[0]": "zero"
}

window.editor = new JSONEditor(container, options, json)

console.log('json', json)
Expand Down
12 changes: 3 additions & 9 deletions examples/20_custom_css_style_for_nodes.html
Expand Up @@ -83,26 +83,20 @@ <h3>Custom class names</h3>

const optionsLeft = {
mode: 'tree',
onError: function (err) {
alert(err.toString())
},
onClassName: onClassName,
onChangeJSON: function (j) {
jsonLeft = j
window.editorRight.refresh()
}
}
}

const optionsRight = {
mode: 'tree',
onError: function (err) {
alert(err.toString())
},
onClassName: onClassName,
onChangeJSON: function (j) {
jsonRight = j
window.editorLeft.refresh()
}
}
}

let jsonLeft = {
Expand Down Expand Up @@ -137,7 +131,7 @@ <h3>Custom class names</h3>
"url": "http://jsoneditoronline.org",
"[0]": "zero"
}

window.editorLeft = new JSONEditor(containerLeft, optionsLeft, jsonLeft)
window.editorRight = new JSONEditor(containerRight, optionsRight, jsonRight)
</script>
Expand Down
5 changes: 3 additions & 2 deletions src/js/JSONEditor.js
Expand Up @@ -329,15 +329,16 @@ JSONEditor.prototype.getMode = function () {

/**
* Throw an error. If an error callback is configured in options.error, this
* callback will be invoked. Else, a regular error is thrown.
* callback will be invoked. Else, a basic alert window with the error message
* will be shown to the user.
* @param {Error} err
* @private
*/
JSONEditor.prototype._onError = function (err) {
if (this.options && typeof this.options.onError === 'function') {
this.options.onError(err)
} else {
throw err
alert(err.toString())
}
}

Expand Down

0 comments on commit cbb95ae

Please sign in to comment.