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

Fix eliminated error message in Init projection handler #2250

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ const buildInit: (
log.debug(`Init handler execution failed. Commit transaction with error`)

await inlineLedgerRunQuery(
`UPDATE ${databaseNameAsId}.${ledgerTableNameAsId}
`ROLLBACK TO SAVEPOINT ${rootSavePointId};

UPDATE ${databaseNameAsId}.${ledgerTableNameAsId}
SET "Errors" = jsonb_insert(
COALESCE("Errors", jsonb('[]')),
CAST(('{' || jsonb_array_length(COALESCE("Errors", jsonb('[]'))) || '}') AS TEXT[]),
Expand Down
59 changes: 59 additions & 0 deletions tests/read-model-store-api/projection-fail-init.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import givenEvents from '@resolve-js/testing-tools'

import { jestTimeout, adapters, adapterFactory } from '../readmodel-test-utils'
import resolvers from './resolvers'

jest.setTimeout(jestTimeout())
// eslint-disable-next-line no-console
console.error = () => {}

const duplicateTableName = 'duplicate-table-name'

describe(`${adapterFactory.name}. Read-model Store API. Projection with failed Init handler`, () => {
beforeEach(adapterFactory.create('fail_init_projection'))
afterEach(adapterFactory.destroy('fail_init_projection'))
const adapter = adapters['fail_init_projection']

test(
[
`Projection Init Handler`,
` store.defineTable(/* tableName: "duplicate-table-name" ... */)`,
` store.defineTable(/* tableName: "duplicate-table-name" ... */)`,
`Should persist Init error in ledger`,
].join('\n'),
async () => {
const projection = {
Init: async (store) => {
await store.defineTable(duplicateTableName, {
indexes: { testId: 'string' },
fields: [],
})

await store.defineTable(duplicateTableName, {
indexes: { testId: 'string' },
fields: [],
})
},
}

try {
await givenEvents([])
.readModel({
name: 'StoreApi',
projection,
resolvers,
})
.withAdapter(adapter)
.query('count', {
testId: 'testId',
projection: { testId: 1 },
})

return Promise.reject('Test failed')
} catch (error) {
expect(error).toBeInstanceOf(Error)
expect(error.message).toContain(duplicateTableName)
}
}
)
})