diff --git a/packages/core/src/connectors/aliyun-dm/index.ts b/packages/core/src/connectors/aliyun-dm/index.ts index 131a233fe..15d44f06c 100644 --- a/packages/core/src/connectors/aliyun-dm/index.ts +++ b/packages/core/src/connectors/aliyun-dm/index.ts @@ -1,5 +1,7 @@ import { z } from 'zod'; +import assertThat from '@/utils/assert-that'; + import { ConnectorError, ConnectorErrorCodes, @@ -61,12 +63,13 @@ export const sendMessage: EmailSendMessageFunction = async (address, type, data) const { accessKeyId, accessKeySecret, accountName, fromAlias, templates } = config; const template = templates.find((template) => template.usageType === type); - if (!template) { - throw new ConnectorError( + assertThat( + template, + new ConnectorError( ConnectorErrorCodes.TemplateNotFound, `Cannot find template for type: ${type}` - ); - } + ) + ); return singleSendMail( { diff --git a/packages/core/src/connectors/aliyun-sms/index.ts b/packages/core/src/connectors/aliyun-sms/index.ts index 222d5de17..4a35da6be 100644 --- a/packages/core/src/connectors/aliyun-sms/index.ts +++ b/packages/core/src/connectors/aliyun-sms/index.ts @@ -1,5 +1,7 @@ import { z } from 'zod'; +import assertThat from '@/utils/assert-that'; + import { ConnectorError, ConnectorErrorCodes, @@ -84,9 +86,10 @@ export const sendMessage: SmsSendMessageFunction = async (phone, type, { code }) const { accessKeyId, accessKeySecret, signName, templates } = config; const template = templates.find(({ usageType }) => usageType === type); - if (!template) { - throw new ConnectorError(ConnectorErrorCodes.TemplateNotFound, `Cannot find template!`); - } + assertThat( + template, + new ConnectorError(ConnectorErrorCodes.TemplateNotFound, `Cannot find template!`) + ); return sendSms( { diff --git a/packages/core/src/connectors/github/index.ts b/packages/core/src/connectors/github/index.ts index efa70f20d..ee83eb939 100644 --- a/packages/core/src/connectors/github/index.ts +++ b/packages/core/src/connectors/github/index.ts @@ -2,6 +2,8 @@ import got, { RequestError as GotRequestError } from 'got'; import { stringify } from 'query-string'; import { z } from 'zod'; +import assertThat from '@/utils/assert-that'; + import { ConnectorMetadata, GetAccessToken, @@ -78,9 +80,7 @@ export const getAccessToken: GetAccessToken = async (code) => { }) .json(); - if (!accessToken) { - throw new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid); - } + assertThat(accessToken, new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid)); return accessToken; }; diff --git a/packages/core/src/connectors/google/index.ts b/packages/core/src/connectors/google/index.ts index 58a0aa7bc..a00294f21 100644 --- a/packages/core/src/connectors/google/index.ts +++ b/packages/core/src/connectors/google/index.ts @@ -8,6 +8,8 @@ import got, { RequestError as GotRequestError } from 'got'; import { stringify } from 'query-string'; import { z } from 'zod'; +import assertThat from '@/utils/assert-that'; + import { ConnectorError, ConnectorErrorCodes, @@ -88,9 +90,7 @@ export const getAccessToken: GetAccessToken = async (code, redirectUri) => { }) .json(); - if (!accessToken) { - throw new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid); - } + assertThat(accessToken, new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid)); return accessToken; };