stacker.news/api/resolvers/serial.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-05-20 01:09:32 +00:00
const { UserInputError } = require('apollo-server-micro')
2021-05-20 17:21:11 +00:00
const retry = require('async-retry')
2021-05-20 01:09:32 +00:00
async function serialize (models, call) {
return await retry(async bail => {
2021-05-20 17:21:11 +00:00
try {
const [, result] = await models.$transaction([
models.$executeRaw(SERIALIZE),
call
])
return result
} catch (error) {
console.log(error)
if (error.message.includes('SN_INSUFFICIENT_FUNDS')) {
bail(new UserInputError('insufficient funds'))
}
if (error.message.includes('SN_NOT_SERIALIZABLE')) {
bail(new Error('wallet balance transaction is not serializable'))
}
if (error.message.includes('SN_CONFIRMED_WITHDRAWL_EXISTS')) {
bail(new Error('withdrawl invoice already confirmed (to withdrawl again create a new invoice)'))
}
if (error.message.includes('SN_PENDING_WITHDRAWL_EXISTS')) {
bail(new Error('withdrawl invoice exists and is pending'))
}
2021-05-20 17:21:11 +00:00
if (error.message.includes('40001')) {
throw new Error('wallet balance serialization failure - retry again')
}
bail(error)
2021-05-20 01:09:32 +00:00
}
2021-05-20 17:21:11 +00:00
}, {
minTimeout: 100,
factor: 1.1,
retries: 5
})
2021-05-20 01:09:32 +00:00
}
const SERIALIZE = 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE'
module.exports = serialize