Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIXME: Handle Invalid Sequence #5

Open
aljones15 opened this issue Jun 9, 2022 · 0 comments
Open

FIXME: Handle Invalid Sequence #5

aljones15 opened this issue Jun 9, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@aljones15
Copy link
Contributor

export async function initializeTransaction({
exchangeInstance, stepId, explain = false
} = {}) {
assert.object(exchangeInstance, 'exchangeInstance');
assert.string(stepId, 'stepId');
assert.bool(explain, 'explain');
const exchangeInstanceId = decodeId({id: exchangeInstance.id});
const transactionId = await generateId();
const collection = database.collections[COLLECTION_NAME];
const query = {
'exchangeInstance.id': exchangeInstanceId,
'exchangeInstance.sequence': exchangeInstance.sequence
};
const $inc = {
'exchangeInstance.sequence': 1,
};
const $set = {
'meta.updated': Date.now(),
[`exchangeInstance.steps.${stepId}`]: {
transactionId,
data: null
}
};
const options = {
...database.writeOptions,
// return document after the update
returnDocument: 'after'
};
if(explain) {
// 'find().limit(1)' is used here because 'updateOne()' doesn't return a
// cursor which allows the use of the explain function.
const cursor = await collection.find(query).limit(1);
return cursor.explain('executionStats');
}
// FIXME: Handle exceptions due to invalid sequence
const result = await collection.findOneAndUpdate(
query, {$inc, $set}, options);
if(!result.value) {
// no document found, exchange instance invalid or expired
throw new BedrockError(
'Invalid or expired exchange instance.', 'DataError', {
httpStatusCode: 400,
public: true
});
}
return transactionId;
}

// FIXME: Handle exceptions due to invalid sequence
const result = await collection.findOneAndUpdate(
query, {$inc, $set}, options);
if(!result.value) {
// no document found, exchange instance invalid or expired
throw new BedrockError(
'Invalid or expired exchange instance.', 'DataError', {
httpStatusCode: 400,
public: true
});
}
return result.value;
}

Two functions need a pattern for handling invalid sequence numbers:

  • storeResponseForExchangeStep
  • initializeTransaction
@aljones15 aljones15 changed the title FIXME: Handle invalid Sequence FIXME: Handle Invalid Sequence Jun 9, 2022
@aljones15 aljones15 added the bug Something isn't working label Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant