Skip to content

Commit

Permalink
fix: catch invalid config on save before writing file (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakowenko committed Sep 10, 2021
1 parent 2a76294 commit e4503e8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
16 changes: 12 additions & 4 deletions api/src/controllers/config.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const fs = require('fs');
const yaml = require('js-yaml');
const redact = require('../util/redact-secrets.util');
const config = require('../constants/config');
const { BAD_REQUEST } = require('../constants/http-status');
const { STORAGE } = require('../constants');

module.exports.get = async (req, res) => {
Expand All @@ -16,8 +18,14 @@ module.exports.get = async (req, res) => {
};

module.exports.patch = async (req, res) => {
const isLegacyPath = fs.existsSync('./config.yml');
const { code } = req.body;
fs.writeFileSync(isLegacyPath ? './config.yml' : `${STORAGE.CONFIG.PATH}/config.yml`, code);
res.send(req.body);
try {
const isLegacyPath = fs.existsSync('./config.yml');
const { code } = req.body;
yaml.load(code);
fs.writeFileSync(isLegacyPath ? './config.yml' : `${STORAGE.CONFIG.PATH}/config.yml`, code);
res.send();
} catch (error) {
if (error.name === 'YAMLException') return res.status(BAD_REQUEST).send(error);
res.send(error);
}
};
1 change: 1 addition & 0 deletions frontend/src/views/Config.vue
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export default {
delete this.frigate.status;
this.waitForRestart();
} catch (error) {
this.loading = false;
this.emitter.emit('error', error);
}
},
Expand Down

0 comments on commit e4503e8

Please sign in to comment.