fix: json parse crashes in monitor model with safe parsing#6767
fix: json parse crashes in monitor model with safe parsing#6767CommanderStorm merged 6 commits intolouislam:masterfrom
Conversation
bb4dadd to
817b51f
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes crashes caused by invalid JSON in the database for Kafka and RabbitMQ monitor configurations by introducing safe JSON parsing with fallback to default values.
Changes:
- Added a
safeJsonParsestatic method to handle JSON parsing errors gracefully - Replaced all direct
JSON.parse()calls for Kafka and RabbitMQ monitor fields with safe parsing - Implemented error logging when JSON parsing fails
server/model/monitor.js
Outdated
| rabbitmqNodes: JSON.parse(this.rabbitmqNodes), | ||
| conditions: JSON.parse(this.conditions), | ||
| rabbitmqNodes: Monitor.safeJsonParse(this.rabbitmqNodes, [], "rabbitmqNodes"), | ||
| conditions: Monitor.safeJsonParse(this.conditions, null, "conditions"), |
There was a problem hiding this comment.
Using null as the default value for conditions may be inconsistent with other fields that use empty arrays or objects. Consider using an empty array [] or empty object {} as the default depending on the expected data structure for conditions, to prevent potential null reference issues in code that consumes this field.
| conditions: Monitor.safeJsonParse(this.conditions, null, "conditions"), | |
| conditions: Monitor.safeJsonParse(this.conditions, [], "conditions"), |
d57a19a to
4abb527
Compare
|
how did the invalid json end up in the db in the first place? I think a better place to fix this is |
5db49ce to
c8a58cf
Compare
|
@dharunashokkumar congrats on your first contribution to Uptime Kuma! 🐻 |
fixes json parse crashes when invalid json is stored in database for kafka and rabbitmq monitors.
added safe json parsing with fallback to default values instead of crashing.