Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with Error Catching in K6 Scripts When Using function and Arrow Functions (() => {}) #4518

Open
Nuoky opened this issue Feb 7, 2025 · 0 comments
Assignees

Comments

@Nuoky
Copy link

Nuoky commented Feb 7, 2025

Brief summary

When using an arrow function under specific conditions and calling "this", the try - catch within the context cannot catch the error normally.

k6 version

0.55

OS

Ubuntu

Docker version and image (if applicable)

No response

Steps to reproduce the problem

script.js:

export class DEMO {
    constructor() {
        this.any = ''
    }

    ok_demo = () => {
        const error_demo = () => {
            console.log('The problem will be triggered as long as any "this" is called.', this.any)
        };

        try {
            throw(new Error("test1"))
        } catch (error) {
            console.log("this", this)
            console.log("error", error)
            console.log("error.message", error.message)
        }
    };

    issue_demo = function () {
        const error_demo = () => {
            console.log('The problem will be triggered as long as any "this" is called.', this.any)
        };

        try {
            throw(new Error("test2"))
        } catch (error) {
            console.log("this", this)
            console.log("error", error)
            console.log("error.message", error.message)
        }
    } 
}


export default async () => {
    const demo = new DEMO();
    await demo.ok_demo();

    console.log('--------------------------------------------')

    await demo.issue_demo();
}

run:

k6 run script.js

You can see that the captured error is actually "this" itself.

Expected behaviour

output:

INFO[0000] this {"any":""}                               source=console
INFO[0000] error {}                                      source=console
INFO[0000] error.message test1                           source=console
INFO[0000] --------------------------------------------  source=console
INFO[0000] this {"any":""}                               source=console
INFO[0000] error {}                                      source=console
INFO[0000] error.message test2                           source=console

Actual behaviour

output:

INFO[0000] this {"any":""}                               source=console
INFO[0000] error {}                                      source=console
INFO[0000] error.message test1                           source=console
INFO[0000] --------------------------------------------  source=console
INFO[0000] this {"any":""}                               source=console
INFO[0000] error {"any":""}                             source=console
INFO[0000] error.message undefined                      source=console
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants