Skip to content

Commit

Permalink
fix: an error when logging undefined inside module
Browse files Browse the repository at this point in the history
  • Loading branch information
niekcandaele committed Jan 3, 2025
1 parent d95c453 commit 9ae1fb0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/app-api/src/executors/executeFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,12 @@ export async function executeFunction(
cleanMsg = JSON.stringify(cleanMsg);
}

if (cleanMsg.length > 1000) {
cleanMsg = cleanMsg.substring(0, 1000) + '...';
if (cleanMsg === undefined) {
cleanMsg = 'undefined';
}

if (!cleanMsg.length) {
cleanMsg = 'Empty log';
if (cleanMsg.length > 1000) {
cleanMsg = cleanMsg.substring(0, 1000) + '...';
}

return new TakaroEventFunctionLog({ ...rawLog, msg: cleanMsg });
Expand Down
50 changes: 50 additions & 0 deletions packages/lib-modules/src/__tests__/bugRepros.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,56 @@ const tests = [
expect(msgs).to.include.members(['foo', JSON.stringify({ foo: 'bar' }), JSON.stringify(['baz', 1])]);
},
}),
/**
* Repro for `console.log`ging `undefined`
*/
new IntegrationTest<IModuleTestsSetupData>({
group,
snapshot: false,
name: 'Bug repro: console.log in a function works with undefined and null objects',
setup: modulesTestSetup,
test: async function () {
const mod = (
await this.client.module.moduleControllerCreate({
name: 'Test module',
})
).data.data;
await this.client.hook.hookControllerCreate({
name: 'Test hook 1',
moduleId: mod.id,
regex: 'test msg',
eventType: 'chat-message',
function: `import { data, takaro } from '@takaro/helpers';
async function main() {
console.log(undefined);
console.log(null);
}
await main();`,
});

await this.client.gameserver.gameServerControllerInstallModule(this.setupData.gameserver.id, mod.id);

const listener = (await new EventsAwaiter().connect(this.client)).waitForEvents(HookEvents.HOOK_EXECUTED, 1);

await this.client.hook.hookControllerTrigger({
eventType: 'chat-message',
gameServerId: this.setupData.gameserver.id,
moduleId: mod.id,
playerId: this.setupData.players[0].id,
eventMeta: {
msg: 'test msg',
channel: EventChatMessageChannelEnum.Global,
},
});

const result = (await listener)[0].data.meta.result;
expect(result.success).to.be.true;
const logs = result.logs;
const msgs = logs.map((l: any) => l.msg);
expect(msgs[0]).to.be.eq('undefined');
expect(msgs[1]).to.be.eq('null');
},
}),
/**
* Repro for https://github.com/gettakaro/takaro/issues/1047
* System config uses the names of functions to create structure
Expand Down

0 comments on commit 9ae1fb0

Please sign in to comment.