Skip to content

Commit

Permalink
Merge pull request #35 from sailpoint-oss/fning/after-handler
Browse files Browse the repository at this point in the history
PLTCONN-3848: Fix error handling for connector and after handler process
  • Loading branch information
fangming-ning-sp committed Sep 15, 2023
2 parents 330c5e7 + d3503b7 commit 3cb23ba
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
57 changes: 57 additions & 0 deletions lib/connector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,63 @@ describe('connector errors', () => {
expect(e).toStrictEqual(new Error(`unsupported command: ${StandardCommand.StdTestConnection}`))
}
})

it('should connector error be handled gracefully', async () => {
const connector = createConnector()
.stdTestConnection(async (context, input, res) => {
throw new Error('Error from connector')
})

try {
await connector._exec(StandardCommand.StdTestConnection, MOCK_CONTEXT, undefined,
new PassThrough({ objectMode: true }).on('data', (chunk) => fail('no data should be received here')))
fail('connector execution should not work')
} catch (e) {
expect(e).toStrictEqual(new Error('Error from connector'))
}
})

it('should customizer before handler error be handled gracefully', async () => {
const connector = createConnector()
.stdTestConnection(async (context, input, res) => {
res.send({})
})

const customizer = createConnectorCustomizer()
.beforeStdTestConnection(async (context, input) => {
throw new Error('Error from customizer after handler')
})

try {
await connector._exec(StandardCommand.StdTestConnection, MOCK_CONTEXT, undefined,
new PassThrough({ objectMode: true }).on('data', (chunk) => fail('no data should be received here')), customizer)

fail('connector execution should not work')
} catch (e) {
expect(e).toStrictEqual(new Error('Error from customizer after handler'))
}
})

it('should customizer after handler error be handled gracefully', async () => {
const connector = createConnector()
.stdTestConnection(async (context, input, res) => {
res.send({})
})

const customizer = createConnectorCustomizer()
.afterStdTestConnection(async (context, output) => {
throw new Error('Error from customizer before handler')
})

try {
await connector._exec(StandardCommand.StdTestConnection, MOCK_CONTEXT, undefined,
new PassThrough({ objectMode: true }).on('data', (chunk) => fail('no data should be received here')), customizer)

fail('connector execution should not work')
} catch (e) {
expect(e).toStrictEqual(new Error('Error from customizer before handler'))
}
})
})

describe('read config', () => {
Expand Down
15 changes: 10 additions & 5 deletions lib/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ export class Connector {
writableObjectMode: true,
async transform(rawResponse: RawResponse, encoding: BufferEncoding, callback: TransformCallback) {
if (rawResponse.type == ResponseType.Output) {
rawResponse.data = await afterHandler!(context, rawResponse.data)
try {
rawResponse.data = await afterHandler!(context, rawResponse.data)
} catch (e: any) {
callback(e)
}
}

res.write(rawResponse)
Expand All @@ -240,18 +244,19 @@ export class Connector {
// We need to wait on the interceptor to be done writting and flushing before we resolve the promise. If we don't wait,
// the interceptor could be ended but is still flushing while this _exec method is resolved. That would cause the writable
// stream that get passed into this _exec method to end as well, and then receive another write call, causing that stream to fail.
return new Promise<void>(async (resolve, reject) => {
let interceptorComplete = new Promise<void>((resolve, reject) => {
resInterceptor.on('finish', function(){
resolve()
})

resInterceptor.on('error', function (e) {
reject(e)
})

await handler(context, input, new ResponseStream<any>(resInterceptor))
resInterceptor.end()
})

await handler(context, input, new ResponseStream<any>(resInterceptor))
resInterceptor.end()
await interceptorComplete
})

}
Expand Down

0 comments on commit 3cb23ba

Please sign in to comment.