Skip to content
This repository has been archived by the owner on Apr 3, 2023. It is now read-only.

Commit

Permalink
refactor: honor social vendor server response code
Browse files Browse the repository at this point in the history
  • Loading branch information
darcyYe committed Mar 19, 2023
1 parent 7375363 commit 300b075
Show file tree
Hide file tree
Showing 19 changed files with 49 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/connector-alipay-native/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('getAccessToken', () => {
await expect(
getAccessToken('code', mockedAlipayNativeConfigWithValidPrivateKey)
).rejects.toMatchError(
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, 'accessToken is missing.')
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, '`accessToken` is missing.')
);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/connector-alipay-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const getAccessToken = async (code: string, config: AlipayNativeConfig) =
const { access_token: accessToken } = alipay_system_oauth_token_response;
assert(
accessToken,
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, 'accessToken is missing.')
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, '`accessToken` is missing.')
);

return { accessToken };
Expand Down
2 changes: 1 addition & 1 deletion packages/connector-alipay-web/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('getAccessToken', () => {
await expect(
getAccessToken('code', mockedAlipayConfigWithValidPrivateKey)
).rejects.toMatchError(
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, 'accessToken is missing.')
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, '`accessToken` is missing.')
);
});

Expand Down
14 changes: 6 additions & 8 deletions packages/connector-azuread/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,12 @@ const getUserInfo =
if (error instanceof HTTPError) {
const { statusCode, body: rawBody } = error.response;

if (statusCode === 401) {
throw new ConnectorError(
ConnectorErrorCodes.AuthorizationFailed,
JSON.stringify(rawBody)
);
}

throw new ConnectorError(ConnectorErrorCodes.General, JSON.stringify(rawBody));
throw new ConnectorError(
statusCode === 401
? ConnectorErrorCodes.AuthorizationFailed
: ConnectorErrorCodes.General,
JSON.stringify({ rawBody, statusCode })
);
}

throw error;
Expand Down
2 changes: 1 addition & 1 deletion packages/connector-discord/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('Discord connector', () => {
set: jest.fn(),
get: jest.fn(),
})
).rejects.toMatchError(new ConnectorError(ConnectorErrorCodes.General, JSON.stringify('')));
).rejects.toMatchError(new ConnectorError(ConnectorErrorCodes.General));
});

it('throws unrecognized error', async () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/connector-discord/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const getAccessToken = async (

const getUserInfo =
(getConfig: GetConnectorConfig): GetUserInfo =>
// eslint-disable-next-line complexity
async (data) => {
const { code, redirectUri } = await authorizationCallbackHandler(data);
const config = await getConfig(defaultMetadata.id);
Expand Down Expand Up @@ -131,7 +132,10 @@ const getUserInfo =
if (error instanceof HTTPError) {
const { body: rawBody } = error.response;

throw new ConnectorError(ConnectorErrorCodes.General, JSON.stringify(rawBody));
throw new ConnectorError(
ConnectorErrorCodes.General,
conditional(rawBody && JSON.stringify(rawBody))
);
}

throw error;
Expand Down
2 changes: 1 addition & 1 deletion packages/connector-github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const getAccessToken = async (config: GithubConfig, codeObject: { code: s

assert(
accessToken,
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, 'accessToken is missing.')
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, '`accessToken` is missing.')
);

return { accessToken };
Expand Down
7 changes: 5 additions & 2 deletions packages/connector-google/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('google connector', () => {
await expect(
getAccessToken(mockedConfig, { code: 'code', redirectUri: 'dummyRedirectUri' })
).rejects.toMatchError(
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, 'accessToken is missing.')
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, '`accessToken` is missing.')
);
});
});
Expand Down Expand Up @@ -116,7 +116,10 @@ describe('google connector', () => {
get: jest.fn(),
})
).rejects.toMatchError(
new ConnectorError(ConnectorErrorCodes.SocialAccessTokenInvalid, JSON.stringify(''))
new ConnectorError(
ConnectorErrorCodes.SocialAccessTokenInvalid,
JSON.stringify({ statusCode: 401, rawBody: '' })
)
);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/connector-google/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const getAccessToken = async (

assert(
accessToken,
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, 'accessToken is missing.')
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, '`accessToken` is missing.')
);

return { accessToken };
Expand Down Expand Up @@ -141,7 +141,7 @@ const getUserInfoErrorHandler = (error: unknown) => {
statusCode === 401
? ConnectorErrorCodes.SocialAccessTokenInvalid
: ConnectorErrorCodes.General,
JSON.stringify(rawBody)
JSON.stringify({ statusCode, rawBody })
);
}

Expand Down
7 changes: 5 additions & 2 deletions packages/connector-kakao/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('kakao connector', () => {
await expect(
getAccessToken(mockedConfig, { code: 'code', redirectUri: 'dummyRedirectUri' })
).rejects.toMatchError(
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, 'accessToken is missing.')
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, '`accessToken` is missing.')
);
});
});
Expand Down Expand Up @@ -120,7 +120,10 @@ describe('kakao connector', () => {
get: jest.fn(),
})
).rejects.toMatchError(
new ConnectorError(ConnectorErrorCodes.SocialAccessTokenInvalid, JSON.stringify(''))
new ConnectorError(
ConnectorErrorCodes.SocialAccessTokenInvalid,
JSON.stringify({ statusCode: 401, rawBody: '' })
)
);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/connector-kakao/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const getAccessToken = async (

assert(
accessToken,
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, 'accessToken is missing.')
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, '`accessToken` is missing.')
);

return { accessToken };
Expand Down Expand Up @@ -144,7 +144,7 @@ const getUserInfoErrorHandler = (error: unknown) => {
statusCode === 401
? ConnectorErrorCodes.SocialAccessTokenInvalid
: ConnectorErrorCodes.General,
JSON.stringify(rawBody)
JSON.stringify({ statusCode, rawBody })
);
}

Expand Down
7 changes: 5 additions & 2 deletions packages/connector-naver/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('naver connector', () => {
await expect(
getAccessToken(mockedConfig, { code: 'code', redirectUri: 'dummyRedirectUri' })
).rejects.toMatchError(
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, 'accessToken is missing.')
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, '`accessToken` is missing.')
);
});
});
Expand Down Expand Up @@ -122,7 +122,10 @@ describe('naver connector', () => {
get: jest.fn(),
})
).rejects.toMatchError(
new ConnectorError(ConnectorErrorCodes.SocialAccessTokenInvalid, JSON.stringify(''))
new ConnectorError(
ConnectorErrorCodes.SocialAccessTokenInvalid,
JSON.stringify({ statusCode: 401, rawBody: '' })
)
);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/connector-naver/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const getAccessToken = async (

assert(
accessToken,
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, 'accessToken is missing.')
new ConnectorError(ConnectorErrorCodes.SocialAuthCodeInvalid, '`accessToken` is missing.')
);

return { accessToken };
Expand Down Expand Up @@ -140,7 +140,7 @@ const getUserInfoErrorHandler = (error: unknown) => {
statusCode === 401
? ConnectorErrorCodes.SocialAccessTokenInvalid
: ConnectorErrorCodes.General,
JSON.stringify(rawBody)
JSON.stringify({ statusCode, rawBody })
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/connector-sendgrid-email/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const sendMessage =
typeof rawBody === 'string',
new ConnectorError(
ConnectorErrorCodes.InvalidResponse,
`wrong response body type: ${typeof rawBody}`
`Invalid response raw body type: ${typeof rawBody}`
)
);

Expand Down
2 changes: 1 addition & 1 deletion packages/connector-twilio-sms/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const sendMessage =
typeof rawBody === 'string',
new ConnectorError(
ConnectorErrorCodes.InvalidResponse,
`wrong response body type: ${typeof rawBody}`
`Invalid response raw body type: ${typeof rawBody}`
)
);

Expand Down
5 changes: 4 additions & 1 deletion packages/connector-wechat-native/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ describe('getUserInfo', () => {
await expect(
connector.getUserInfo({ code: 'code' }, jest.fn(), { set: jest.fn(), get: jest.fn() })
).rejects.toMatchError(
new ConnectorError(ConnectorErrorCodes.SocialAccessTokenInvalid, JSON.stringify(''))
new ConnectorError(
ConnectorErrorCodes.SocialAccessTokenInvalid,
JSON.stringify({ statusCode: 401, rawBody: '' })
)
);
});
});
2 changes: 1 addition & 1 deletion packages/connector-wechat-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const getUserInfoErrorHandler = (error: unknown) => {
statusCode === 401
? ConnectorErrorCodes.SocialAccessTokenInvalid
: ConnectorErrorCodes.General,
JSON.stringify(rawBody)
JSON.stringify({ statusCode, rawBody })
);
}

Expand Down
5 changes: 4 additions & 1 deletion packages/connector-wechat-web/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ describe('getUserInfo', () => {
await expect(
connector.getUserInfo({ code: 'code' }, jest.fn(), { set: jest.fn(), get: jest.fn() })
).rejects.toMatchError(
new ConnectorError(ConnectorErrorCodes.SocialAccessTokenInvalid, JSON.stringify(''))
new ConnectorError(
ConnectorErrorCodes.SocialAccessTokenInvalid,
JSON.stringify({ statusCode: 401, rawBody: '' })
)
);
});
});
2 changes: 1 addition & 1 deletion packages/connector-wechat-web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const getUserInfoErrorHandler = (error: unknown) => {
statusCode === 401
? ConnectorErrorCodes.SocialAccessTokenInvalid
: ConnectorErrorCodes.General,
JSON.stringify(rawBody)
JSON.stringify({ statusCode, rawBody })
);
}

Expand Down

0 comments on commit 300b075

Please sign in to comment.