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

I've made "throw expression" package #15

Open
golergka opened this issue Jun 29, 2022 · 7 comments
Open

I've made "throw expression" package #15

golergka opened this issue Jun 29, 2022 · 7 comments

Comments

@golergka
Copy link

Since this proposal is still not implemented, and even using transpilers would break a lot of other tools working with the code, I've made a tiny package, thrw, and have been using it in a lot of projects, both personal and professional.

I want to share this package with other people. Would it be a good idea to add a link to it somwhere in this repository?

@ljharb
Copy link
Member

ljharb commented Jun 29, 2022

Doesn't that change the stack trace?

@rbuckton
Copy link
Collaborator

Doesn't that change the stack trace?

It depends on the implementation I suppose (of both the library and the runtime), since .stack isn't specified.

In v8, you get the correct stack trace in .stack, but if your debugger is set to stop on all exceptions it will still pause inside the thrw function:

function f() {
    g();
}

function g() {
    thrw(new Error());
}

function thrw(e) { throw e; }

try {
    f();
}
catch (e) {
    console.log(e.stack);
    // Error
    //     at g (C:\scratch\thrw.js:6:10)
    //     at f (C:\scratch\thrw.js:2:5)
    //     at Object.<anonymous> (C:\scratch\thrw.js:12:5)
    //     at Module._compile (node:internal/modules/cjs/loader:1101:14)
    //     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    //     at Module.load (node:internal/modules/cjs/loader:981:32)
    //     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    //     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    //     at node:internal/main/run_main_module:17:47
}

In Chakra, .stack wasn't populated until the exception was thrown.

@ljharb
Copy link
Member

ljharb commented Jun 29, 2022

ah, yes, good point - if you make the error object inline in the thrw call then it'd be fine.

@Xunnamius
Copy link

I prefer the toss terminology myself 😉

@golergka
Copy link
Author

I prefer the toss terminology myself 😉

Love seeing that I'm not the only one with the same idea!

@JC3
Copy link

JC3 commented Jul 5, 2022

Usually I just use something like:

const fail (msg) = msg => { throw Error(msg); } // messes with stack trace
fail('oops');

Or

const fail (e) = e => { throw e; }
fail(Error('oops'));

Or, my preference:

Error.prototype.Throw  = function () { throw this; }
Error('oops').Throw();

It's about the same amount of code as import fail from 'fail'. :)

@parzhitsky
Copy link

@JC3 Error(…).Throw() is neat (got me interested), but there's so much wrong with it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants