sindresorhus / ow

Function argument validation for humans
https://sindresorhus.com/ow/
MIT License
3.8k stars 105 forks source link

Printed trace is confusing #178

Closed mnmkng closed 3 years ago

mnmkng commented 4 years ago

When using ow with Node 12 and an unhandled exception occurs, the following is printed to the console:

I'm aware that this is how Node prints errors. I'm wondering if there's a way to make it better with ow.

/Users/mnmkng/Projects/Apify/apify-client-js/node_modules/ow/dist/source/index.js:56
                throw"function" == typeof r && (s = r()), s = s ? `${this.type} \`${s}\`` : this.type, new n.ArgumentError(i(e, s, o), t)
                ^

a [ArgumentError]: Expected `options` to be of type `object` but received type `undefined`
    at u (/Users/mnmkng/Projects/Apify/apify-client-js/node_modules/ow/dist/source/index.js:274:22)
    at new ApiClient (/Users/mnmkng/Projects/Apify/apify-client-js/src/base/api_client.js:16:9)
    at new ResourceClient (/Users/mnmkng/Projects/Apify/apify-client-js/src/base/resource_client.js:18:9)
    at new ActorClient (/Users/mnmkng/Projects/Apify/apify-client-js/src/resources/actor.js:12:9)
    at Object.<anonymous> (/Users/mnmkng/Projects/Apify/apify-client-js/src/resources/actor.js:16:1)
    at Module._compile (internal/modules/cjs/loader.js:1157:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
    at Module.load (internal/modules/cjs/loader.js:1001:32)
    at Function.Module._load (internal/modules/cjs/loader.js:900:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
  name: 'ArgumentError'
}

I find the first two lines confusing and obfuscating the actual argument validation message. There's also an extra a before the [ArgumentError]: and the first line in the stack trace does not point to the location of the validation problem.

Is there a way to clean this up a bit? I played with removing extra lines from the stack and rethrowing but the solution was not ideal, but I guess a tiny bit better than before. I could not find a way to programmatically generate the first part of the stack (the one showing piece of code). Maybe you know more?

try {
    new ApiClient() // uses ow validation
} catch (err) {
    const newStack = err.stack.split('\n')
    newStack.splice(1, 1)
    err.stack = newStack.join('\n');
    throw err
}
/Users/mnmkng/Projects/Apify/apify-client-js/src/resources/actor.js:22
    throw err
    ^

a [ArgumentError]: Expected `options` to be of type `object` but received type `undefined`
    at new ApiClient (/Users/mnmkng/Projects/Apify/apify-client-js/src/base/api_client.js:16:9)
    at new ResourceClient (/Users/mnmkng/Projects/Apify/apify-client-js/src/base/resource_client.js:18:9)
    at new ActorClient (/Users/mnmkng/Projects/Apify/apify-client-js/src/resources/actor.js:12:9)
    at Object.<anonymous> (/Users/mnmkng/Projects/Apify/apify-client-js/src/resources/actor.js:17:5)
    at Module._compile (internal/modules/cjs/loader.js:1157:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
    at Module.load (internal/modules/cjs/loader.js:1001:32)
    at Function.Module._load (internal/modules/cjs/loader.js:900:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
  name: 'ArgumentError'
}
sindresorhus commented 4 years ago

For some reason, the code is being minified at build time. That's why the stack trace looks ugly. It didn't used to be minified, so I'm not sure what changed.

sindresorhus commented 3 years ago

This should be fixed by https://github.com/sindresorhus/ow/releases/tag/v0.20.0