fix reverting skill on failed update
This commit is contained in:
@@ -17,7 +17,7 @@ constants.apiResultCodes = {
|
|||||||
AMAZON_FAIL:2, //amazon api doesn't work
|
AMAZON_FAIL:2, //amazon api doesn't work
|
||||||
DATABASE_ERROR:3,
|
DATABASE_ERROR:3,
|
||||||
NO_SKILL:4,
|
NO_SKILL:4,
|
||||||
INCONSISTEN_STATE:5,
|
INCONSISTENT_STATE:5,
|
||||||
}
|
}
|
||||||
|
|
||||||
constants.HTTPResultCodes = {
|
constants.HTTPResultCodes = {
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ router.put ('/:id', bodyParser.json (), async (req, res, next) => {
|
|||||||
.then (() => {
|
.then (() => {
|
||||||
//Ok, done, now update skill on Amazon (if needed)
|
//Ok, done, now update skill on Amazon (if needed)
|
||||||
if (updateOnAmazon) {
|
if (updateOnAmazon) {
|
||||||
|
//We need to update skill on Amazon
|
||||||
amazonHelper
|
amazonHelper
|
||||||
.updateSkill (skill)
|
.updateSkill (skill)
|
||||||
.then (amazonResult => {
|
.then (amazonResult => {
|
||||||
@@ -51,45 +52,78 @@ router.put ('/:id', bodyParser.json (), async (req, res, next) => {
|
|||||||
res.json ({result: constants.apiResultCodes.OK, message: ''});
|
res.json ({result: constants.apiResultCodes.OK, message: ''});
|
||||||
alexa.updateModel ();
|
alexa.updateModel ();
|
||||||
} else {
|
} else {
|
||||||
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
|
//Update on amazon failed, revert changes in database and send error to user
|
||||||
|
databaseHelper
|
||||||
|
.updateSkill (id, currentSkillState)
|
||||||
|
.then (() => {
|
||||||
|
res
|
||||||
|
.status (
|
||||||
|
constants.HTTPResultCodes.INTERNAL_SERVER_ERROR
|
||||||
|
)
|
||||||
|
.json ({
|
||||||
result: constants.apiResultCodes.AMAZON_ERROR,
|
result: constants.apiResultCodes.AMAZON_ERROR,
|
||||||
message: amazonResult,
|
message: amazonResult,
|
||||||
});
|
});
|
||||||
|
})
|
||||||
|
.catch (() => {
|
||||||
|
//This should never happen, something is seriously wrong, like no database connection
|
||||||
|
res
|
||||||
|
.status (
|
||||||
|
constants.HTTPResultCodes.INTERNAL_SERVER_ERROR
|
||||||
|
)
|
||||||
|
.json ({
|
||||||
|
result: constants.apiResultCodes.INCONSISTENT_STATE,
|
||||||
|
message: '',
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch (e => {
|
.catch (e => {
|
||||||
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
|
//Update on amazon failed, revert changes in database and send error to user
|
||||||
|
databaseHelper
|
||||||
|
.updateSkill (id, currentSkillState)
|
||||||
|
.then (() => {
|
||||||
|
res
|
||||||
|
.status (constants.HTTPResultCodes.INTERNAL_SERVER_ERROR)
|
||||||
|
.json ({
|
||||||
result: constants.apiResultCodes.AMAZON_FAIL,
|
result: constants.apiResultCodes.AMAZON_FAIL,
|
||||||
message: e,
|
message: e,
|
||||||
});
|
});
|
||||||
|
})
|
||||||
|
.catch (() => {
|
||||||
|
//This should never happen, something is seriously wrong, like no database connection
|
||||||
|
res
|
||||||
|
.status (constants.HTTPResultCodes.INTERNAL_SERVER_ERROR)
|
||||||
|
.json ({
|
||||||
|
result: constants.apiResultCodes.INCONSISTENT_STATE,
|
||||||
|
message: '',
|
||||||
});
|
});
|
||||||
}else{
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
//No need to update on Amazon, tell to user it's ok
|
||||||
res.json ({result: constants.apiResultCodes.OK, message: ''});
|
res.json ({result: constants.apiResultCodes.OK, message: ''});
|
||||||
alexa.updateModel ();
|
alexa.updateModel ();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch (() => {
|
.catch (() => {
|
||||||
//Update in database didn't go well, revert changes
|
//Update in database didn't go well, no need to revert since it failed to write in the first place
|
||||||
databaseHelper
|
//just send error to user
|
||||||
.updateSkill (id, currentSkillState)
|
res
|
||||||
.then (() => {
|
.status (
|
||||||
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
|
constants.HTTPResultCodes.INTERNAL_SERVER_ERROR
|
||||||
|
)
|
||||||
|
.json ({
|
||||||
result: constants.apiResultCodes.DATABASE_ERROR,
|
result: constants.apiResultCodes.DATABASE_ERROR,
|
||||||
message: '',
|
message: '',
|
||||||
});
|
});
|
||||||
})
|
|
||||||
.catch (() => {
|
|
||||||
//This should never happen, something is seriously wrong, like no database connection
|
|
||||||
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
|
|
||||||
result: constants.apiResultCodes.INCONSISTEN_STATE,
|
|
||||||
message: '',
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch (e => {
|
.catch (e => {
|
||||||
//I don't know why, but something went wrong, possibly ID of skill is wrong, doesn't exist in DB
|
//I don't know why, but something went wrong, possibly ID of skill is wrong, doesn't exist in DB
|
||||||
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({result: constants.apiResultCodes.NO_SKILL, message: ''});
|
res
|
||||||
|
.status (constants.HTTPResultCodes.INTERNAL_SERVER_ERROR)
|
||||||
|
.json ({result: constants.apiResultCodes.NO_SKILL, message: ''});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user