Skip to content

Commit 83f3df3

Browse files
committed
maint(pat switch): When logging errors, add the failing element for better debugging. Also, do not log an error twice.
1 parent de13186 commit 83f3df3

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/pat/switch/switch.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ export default Base.extend({
2121

2222
init() {
2323
this.options = this._validateOptions(parser.parse(this.el, this.options, true));
24-
if (!this.options.length) {
25-
log.error("Switch without options cannot be initialized.");
26-
}
2724

2825
events.add_event_listener(this.el, "click", "pat-switch--on-click", (e) => {
2926
if (e.tagName === "A") {
@@ -68,11 +65,24 @@ export default Base.extend({
6865
_validateOptions: function (options) {
6966
var correct = [];
7067

68+
let logged_error = false;
7169
for (var i = 0; i < options.length; i++) {
7270
var option = options[i];
73-
if (option.selector && (option.remove || option.add)) correct.push(option);
74-
else log.error("Switch pattern requires selector and one of add or remove.");
71+
if (option.selector && (option.remove || option.add)) {
72+
correct.push(option);
73+
} else {
74+
log.error(
75+
"Switch pattern requires selector and one of add or remove.",
76+
this.el
77+
);
78+
logged_error = true;
79+
}
7580
}
81+
82+
if (!correct.length && !logged_error) {
83+
log.error("Switch without options cannot be initialized.", this.el);
84+
}
85+
7686
return correct;
7787
},
7888

0 commit comments

Comments
 (0)