Skip to content

Commit

Permalink
Refactor error handling in assets-controller
Browse files Browse the repository at this point in the history
  • Loading branch information
seansica committed Oct 4, 2023
1 parent 3bbc51a commit d2bd15f
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions app/controllers/assets-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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.');
}

}

Check warning on line 65 in app/controllers/assets-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/assets-controller.js#L54-L65

Added lines #L54 - L65 were not covered by tests
Expand All @@ -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.');
}
Expand All @@ -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.');
}
Expand Down

0 comments on commit d2bd15f

Please sign in to comment.