0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00

Refactored Labels API add method back to promises

refs https://github.com/TryGhost/Team/issues/694

- Additional try/catch block needed in async/await implementation increased method complexity  and broke the complexity linting rule. This is a dirty way to fix the warning. Ideally the implementation should stay with async/await syntax and instead move the custom error handling logic into some different layer. For example we could introduce a separate "stage" in the API framework's "pipeline" where we'd catch and handle in a generic way all of the "unique" types of errors. It would make sense to have a generic handler because this same code happens in labels, member and few more places.
This commit is contained in:
Naz 2021-09-02 15:42:27 +04:00
parent 7851e4ce52
commit 8d36ebeb3c
2 changed files with 16 additions and 18 deletions

View file

@ -76,16 +76,15 @@ module.exports = {
}
},
permissions: true,
async query(frame) {
try {
return await models.Label.add(frame.data.labels[0], frame.options);
} catch (error) {
if (error.code && error.message.toLowerCase().indexOf('unique') !== -1) {
throw new errors.ValidationError({message: i18n.t('errors.api.labels.labelAlreadyExists')});
}
query(frame) {
return models.Label.add(frame.data.labels[0], frame.options)
.catch((error) => {
if (error.code && error.message.toLowerCase().indexOf('unique') !== -1) {
throw new errors.ValidationError({message: i18n.t('errors.api.labels.labelAlreadyExists')});
}
throw error;
}
throw error;
});
}
},

View file

@ -76,16 +76,15 @@ module.exports = {
}
},
permissions: true,
async query(frame) {
try {
return await models.Label.add(frame.data.labels[0], frame.options);
} catch (error) {
if (error.code && error.message.toLowerCase().indexOf('unique') !== -1) {
throw new errors.ValidationError({message: i18n.t('errors.api.labels.labelAlreadyExists')});
}
query(frame) {
return models.Label.add(frame.data.labels[0], frame.options)
.catch((error) => {
if (error.code && error.message.toLowerCase().indexOf('unique') !== -1) {
throw new errors.ValidationError({message: i18n.t('errors.api.labels.labelAlreadyExists')});
}
throw error;
}
throw error;
});
}
},