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

os.userInfo is not a function #51

Closed
Ostricker opened this issue Apr 1, 2020 · 5 comments
Closed

os.userInfo is not a function #51

Ostricker opened this issue Apr 1, 2020 · 5 comments

Comments

@Ostricker
Copy link

user : o.user || o.username || auth[0] || env.PGUSERNAME || env.PGUSER || os.userInfo().username,

Node v12.13.0
OS: Windows 10
Usage: inside Electron + React

Anyway this line is giving me error - TypeError: os.userInfo is not a function.

As per SO question and answer -> https://stackoverflow.com/a/56253238 os.userInfo() cant be called from browser.

One solution is deleting os.userInfo().username and replacing it with empty quotes, which i did in my project. User should be able to put username inside env var or options.

@porsager
Copy link
Owner

porsager commented Apr 1, 2020

This will only be called if you don't set the user in any of the other ways. Isn't that an option for you?

const sql = postgres({
  username: 'your user'
})

@Ostricker
Copy link
Author

Yes :) I have only stumbled upon this because I have incorrectly used env variable. But it seems more appropriate to give user some more readable error then this. But do what you will, its not that big of a deal :)

@porsager
Copy link
Owner

porsager commented Apr 1, 2020

Ah I see.. Goodie :)

Doesn't the stack trace give a proper indication of the error? Mind posting the entire stack trace for the error?

It's a lot of custom error handling which might even lead to brittle errors instead if I tried to handle this specific scenario. Do you have ideas for how to handle it better?

@Ostricker
Copy link
Author

Ostricker commented Apr 1, 2020

This is the error:
index.js:463 Uncaught TypeError: os.userInfo is not a function
at parseOptions (index.js:463)
at Postgres (index.js:43)
at new _PgSQLFrame (PgSQLFrame.ts:25)
at Module../src/electrontool/commontools/lib/DBFrames/PgSQLFrame.ts (PgSQLFrame.ts:133)

As for how to handle it:
Option 1: Dont use it :D || os.userInfo().username -> || ''
Option 2: Safe getter -
public getSafeObject(fn: Function): any { try { return fn(); } catch (e) { return ""; }}
and then call it as this - getSafeObject(()=> os.userInfo().username)

I dont think there is other way to handle it without try/catch, so other options are just variants of try and catch.

@porsager
Copy link
Owner

porsager commented Apr 6, 2020

Ah, I see what you mean, that's pretty obvious ;) Fixed

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

2 participants