Skip to content

Commit

Permalink
Improve logging to avoid "TypeError: Converting circular structure to…
Browse files Browse the repository at this point in the history
… JSON"
  • Loading branch information
komiya-atsushi committed Jan 11, 2024
1 parent a3f2bb0 commit 01190bb
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 49 deletions.
8 changes: 6 additions & 2 deletions packages/bolt-s3/src/S3InstallationStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class S3Client {
Body: data,
});

logger?.debug(`S3 putObject response: ${JSON.stringify(response)}`);
logger?.debug(
`S3 putObject response: ${JSON.stringify(response.$metadata)}`
);
}

async fetch(key: string, logger?: Logger): Promise<Buffer | undefined> {
Expand All @@ -35,7 +37,9 @@ class S3Client {
Bucket: this.bucketName,
Key: key,
});
logger?.debug(`S3 getObject response: ${JSON.stringify(response)}`);
logger?.debug(
`S3 getObject response: ${JSON.stringify(response.$metadata)}`
);

const body = response.Body;
if (body === undefined) {
Expand Down
116 changes: 69 additions & 47 deletions packages/bolt-s3/test/S3InstallationStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Installation} from '@slack/oauth';
import {ConsoleLogger, LogLevel} from '@slack/logger';
import {
DeleteObjectsCommandInput,
ListObjectsV2CommandInput,
Expand All @@ -16,6 +17,9 @@ const s3Client = new S3({
},
});

const logger = new ConsoleLogger();
logger.setLevel(LogLevel.DEBUG);

const installationCodec = BinaryInstallationCodec.createDefault(
'test-password',
'test-salt'
Expand Down Expand Up @@ -113,7 +117,7 @@ describe('S3InstallationStore', () => {

describe('storeInstallation()', () => {
test('can store installations with histories', async () => {
await sut.storeInstallation(installation);
await sut.storeInstallation(installation, logger);

const keys = await listObjectKeys(`${slackClientId}/`);

Expand All @@ -134,8 +138,8 @@ describe('S3InstallationStore', () => {

test('can update installer-latest', async () => {
const anotherUserId = 'another-user-id';
await sut.storeInstallation(installation);
await sut.storeInstallation(anotherInstallation);
await sut.storeInstallation(installation, logger);
await sut.storeInstallation(anotherInstallation, logger);

const data = await getObject(
`${slackClientId}/none-${teamId}/installer-latest`
Expand All @@ -150,33 +154,42 @@ describe('S3InstallationStore', () => {

describe('fetchInstallation()', () => {
test('can fetch installer-latest', async () => {
await sut.storeInstallation(installation);

const fetched = await sut.fetchInstallation({
enterpriseId: undefined,
teamId,
isEnterpriseInstall: false,
});
await sut.storeInstallation(installation, logger);

const fetched = await sut.fetchInstallation(
{
enterpriseId: undefined,
teamId,
isEnterpriseInstall: false,
},
logger
);

expect(fetched).toEqual(installation);
});

test('can fetch installer-USERID-latest', async () => {
await sut.storeInstallation(installation);
await sut.storeInstallation(anotherInstallation);

const fetched = await sut.fetchInstallation({
enterpriseId: undefined,
teamId,
userId,
isEnterpriseInstall: false,
});
const fetchedAnotherUser = await sut.fetchInstallation({
enterpriseId: undefined,
teamId,
userId: anotherUserId,
isEnterpriseInstall: false,
});
await sut.storeInstallation(installation, logger);
await sut.storeInstallation(anotherInstallation, logger);

const fetched = await sut.fetchInstallation(
{
enterpriseId: undefined,
teamId,
userId,
isEnterpriseInstall: false,
},
logger
);
const fetchedAnotherUser = await sut.fetchInstallation(
{
enterpriseId: undefined,
teamId,
userId: anotherUserId,
isEnterpriseInstall: false,
},
logger
);

expect(fetched).toEqual(installation);
expect(fetchedAnotherUser).toHaveProperty('user.id', anotherUserId);
Expand All @@ -185,27 +198,33 @@ describe('S3InstallationStore', () => {
test('throws error if installation does not exist', async () => {
await expect(
async () =>
await sut.fetchInstallation({
enterpriseId: undefined,
teamId: 'team-id-does-not-exist',
userId: 'user-id-does-not-exist',
isEnterpriseInstall: false,
})
await sut.fetchInstallation(
{
enterpriseId: undefined,
teamId: 'team-id-does-not-exist',
userId: 'user-id-does-not-exist',
isEnterpriseInstall: false,
},
logger
)
).rejects.toThrow();
});
});

describe('deleteInstallation()', () => {
test('can delete latest installation and histories by userId', async () => {
await sut.storeInstallation(installation);
await sut.storeInstallation(anotherInstallation);

await sut.deleteInstallation({
enterpriseId: undefined,
teamId,
userId,
isEnterpriseInstall: false,
});
await sut.storeInstallation(installation, logger);
await sut.storeInstallation(anotherInstallation, logger);

await sut.deleteInstallation(
{
enterpriseId: undefined,
teamId,
userId,
isEnterpriseInstall: false,
},
logger
);

const keys = await listObjectKeys(`${slackClientId}/`);

Expand Down Expand Up @@ -234,13 +253,16 @@ describe('S3InstallationStore', () => {
});

test('can delete all installations by team', async () => {
await sut.storeInstallation(installation);

await sut.deleteInstallation({
enterpriseId: undefined,
teamId,
isEnterpriseInstall: false,
});
await sut.storeInstallation(installation, logger);

await sut.deleteInstallation(
{
enterpriseId: undefined,
teamId,
isEnterpriseInstall: false,
},
logger
);

const keys = await listObjectKeys(`${slackClientId}/`);

Expand Down

0 comments on commit 01190bb

Please sign in to comment.