post-ephemeral-errors.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { WebAPICallResult } from '@slack/web-api';
  2. import { generateMarkdownSectionBlock } from './block-creater';
  3. import { generateWebClient } from './webclient-factory';
  4. export const postEphemeralErrors = async(
  5. rejectedResults: PromiseRejectedResult[],
  6. channelId: string,
  7. userId: string,
  8. botToken: string,
  9. ): Promise<WebAPICallResult|void> => {
  10. if (rejectedResults.length > 0) {
  11. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  12. const client = generateWebClient(botToken);
  13. return client.chat.postEphemeral({
  14. text: 'Error occured.',
  15. channel: channelId,
  16. user: userId,
  17. blocks: [
  18. generateMarkdownSectionBlock('*Error occured:*'),
  19. ...rejectedResults.map((rejectedResult) => {
  20. const reason = rejectedResult.reason.toString();
  21. const resData = rejectedResult.reason.response?.data;
  22. const resDataMessage = resData?.message || resData?.toString();
  23. let errorMessage = reason;
  24. if (resDataMessage != null) {
  25. errorMessage += `\n Cause: ${resDataMessage}`;
  26. }
  27. return generateMarkdownSectionBlock(errorMessage);
  28. }),
  29. ],
  30. });
  31. }
  32. return;
  33. };