diff --git a/app/controllers/assets-controller.js b/app/controllers/assets-controller.js index f4794cad..c6e2563d 100644 --- a/app/controllers/assets-controller.js +++ b/app/controllers/assets-controller.js @@ -2,6 +2,7 @@ const assetsService = require('../services/assets-service'); const logger = require('../lib/logger'); +const { DuplicateIdError, BadlyFormattedParameterError, InvalidQueryStringParameterError } = require('../exceptions'); exports.retrieveAll = async function(req, res) { const options = { @@ -50,17 +51,15 @@ exports.retrieveById = async function(req, res) { } } catch(err) { - if (err.message === assetsService.errors.badlyFormattedParameter) { + if (err instanceof BadlyFormattedParameterError) { logger.warn('Badly formatted stix id: ' + req.params.stixId); return res.status(400).send('Stix id is badly formatted.'); - } - else if (err.message === assetsService.errors.invalidQueryStringParameter) { + } else if (err instanceof InvalidQueryStringParameterError) { logger.warn('Invalid query string: versions=' + req.query.versions); return res.status(400).send('Query string parameter versions is invalid.'); - } - else { + } else { logger.error('Failed with error: ' + err); - return res.status(500).send('Unable to get assets. Server error.'); + return res.status(500).send('Unable to get matrices. Server error.'); } } @@ -82,7 +81,7 @@ exports.retrieveVersionById = async function(req, res) { } } catch(err) { - if (err.message === assetsService.errors.badlyFormattedParameter) { + if (err instanceof BadlyFormattedParameterError) { logger.warn('Badly formatted stix id: ' + req.params.stixId); return res.status(400).send('Stix id is badly formatted.'); } @@ -108,7 +107,7 @@ exports.create = async function(req, res) { return res.status(201).send(asset); } catch(err) { - if (err.message === assetsService.errors.duplicateId) { + if (err instanceof DuplicateIdError) { logger.warn("Duplicate stix.id and stix.modified"); return res.status(409).send('Unable to create asset. Duplicate stix.id and stix.modified properties.'); }