Skip to content

Commit cbb95ae

Browse files
committed
Change the default behavior of error handling to open a basic alert instead of logging the error in the console (see #1364).
1 parent 883a0c9 commit cbb95ae

File tree

8 files changed

+12
-27
lines changed

8 files changed

+12
-27
lines changed

HISTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ https://github.com/josdejong/jsoneditor
55

66
## not yet published, version 9.5.4
77

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

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Constructs a new JSONEditor.
119119
- `{function} onError(error)`
120120

121121
Set a callback function triggered when an error occurs. Invoked with the error as first argument. The callback is only invoked
122-
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.
122+
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.
123123

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

examples/03_switch_mode.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@
4343
const options = {
4444
mode: 'tree',
4545
modes: ['code', 'form', 'text', 'tree', 'view', 'preview'], // allowed modes
46-
onError: function (err) {
47-
alert(err.toString())
48-
},
4946
onModeChange: function (newMode, oldMode) {
5047
console.log('Mode switched from', oldMode, 'to', newMode)
5148
}

examples/09_readonly_text_mode.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@
5050
return false;
5151
}
5252
},
53-
onError: function (err) {
54-
alert(err.toString())
55-
},
5653
onModeChange: function (newMode, oldMode) {
5754
console.log('Mode switched from', oldMode, 'to', newMode)
5855
}

examples/15_selection_api.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
code.multiline {
2323
display: block;
24-
white-space: pre-wrap
24+
white-space: pre-wrap;
2525
}
2626

2727
#jsoneditor {
@@ -63,9 +63,6 @@
6363
const options = {
6464
mode: 'tree',
6565
modes: ['code', 'form', 'text', 'tree', 'view', 'preview'], // allowed modes
66-
onError: function (err) {
67-
alert(err.toString())
68-
},
6966
onChange: function () {
7067
console.log('change')
7168
},

examples/17_on_event_api.html

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<body>
2929

3030
<p>
31-
When clicking on a JSON field or value, a log message will be shown in
31+
When clicking on a JSON field or value, a log message will be shown in
3232
console.
3333
</p>
3434

@@ -43,9 +43,6 @@
4343
mode: 'tree',
4444
modes: ['code', 'form', 'text', 'tree', 'view', 'preview'], // allowed modes
4545
name: "jsonContent",
46-
onError: function (err) {
47-
alert(err.toString())
48-
},
4946
onEvent: function(node, event) {
5047
if (node.value !== undefined) {
5148
console.log(event.type + ' event ' +
@@ -75,7 +72,7 @@
7572
"url": "http://jsoneditoronline.org",
7673
"[0]": "zero"
7774
}
78-
75+
7976
window.editor = new JSONEditor(container, options, json)
8077

8178
console.log('json', json)

examples/20_custom_css_style_for_nodes.html

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,20 @@ <h3>Custom class names</h3>
8383

8484
const optionsLeft = {
8585
mode: 'tree',
86-
onError: function (err) {
87-
alert(err.toString())
88-
},
8986
onClassName: onClassName,
9087
onChangeJSON: function (j) {
9188
jsonLeft = j
9289
window.editorRight.refresh()
93-
}
90+
}
9491
}
9592

9693
const optionsRight = {
9794
mode: 'tree',
98-
onError: function (err) {
99-
alert(err.toString())
100-
},
10195
onClassName: onClassName,
10296
onChangeJSON: function (j) {
10397
jsonRight = j
10498
window.editorLeft.refresh()
105-
}
99+
}
106100
}
107101

108102
let jsonLeft = {
@@ -137,7 +131,7 @@ <h3>Custom class names</h3>
137131
"url": "http://jsoneditoronline.org",
138132
"[0]": "zero"
139133
}
140-
134+
141135
window.editorLeft = new JSONEditor(containerLeft, optionsLeft, jsonLeft)
142136
window.editorRight = new JSONEditor(containerRight, optionsRight, jsonRight)
143137
</script>

src/js/JSONEditor.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,15 +329,16 @@ JSONEditor.prototype.getMode = function () {
329329

330330
/**
331331
* Throw an error. If an error callback is configured in options.error, this
332-
* callback will be invoked. Else, a regular error is thrown.
332+
* callback will be invoked. Else, a basic alert window with the error message
333+
* will be shown to the user.
333334
* @param {Error} err
334335
* @private
335336
*/
336337
JSONEditor.prototype._onError = function (err) {
337338
if (this.options && typeof this.options.onError === 'function') {
338339
this.options.onError(err)
339340
} else {
340-
throw err
341+
alert(err.toString())
341342
}
342343
}
343344

0 commit comments

Comments
 (0)