bottender icon indicating copy to clipboard operation
bottender copied to clipboard

Catch sendFlex request error in promise

Open aqzhyi opened this issue 5 years ago • 2 comments

Is your feature request related to a problem? Please describe.

A clear and concise description of what the problem is. Ex. I'm always frustrated when

In website requests, commonly I'll catch errors when error status code >= 400

e.g.

try {
  const data = await axios.get('/data').then(response => response.data)
} catch (error) {
  alert(error.message)
}

but on bottender context.sendFlex I get nothing when HTTP/1.1 400 Bad Request

context.sendText({} as any).catch(error => {
  console.info('🔥', error)
})

Describe the solution you'd like

A clear and concise description of what you want to happen.

A simple way of handling sendText error

e.g.

context.sendText({} as any).catch(error => {
  context.sendText(error.message)
})

Describe alternatives you've considered

A clear and concise description of any alternative solutions or features you've considered.

donno

Additional context

Add any other context or screenshots about the feature request here.

  • "bottender": "^1.2.1"

aqzhyi avatar Feb 29 '20 16:02 aqzhyi

Unfortunately, we can't benefit from the typical promise error handling here, because under the hood context.sendText() just push the message to the batch queue. The major reason is that the reply token can only be used once, so we can't wait the response from LINE directly.

await context.sendText('a');
await context.sendText('b');
await context.sendText('c');
await context.sendText('d');
await context.sendText('e');

Something like this will only cause one request to LINE at the end of the context handling. Therefore, I'd recommend you use _error.js to handle the error: https://bottender.js.org/docs/the-basics-errors

chentsulin avatar Mar 01 '20 15:03 chentsulin

Yes, I tried the _error.js before, and doubt that why can't I catch errors in promise.

Ok, thank you for explaining.

aqzhyi avatar Mar 01 '20 16:03 aqzhyi