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

Unhandled electron-log error TypeError: The "path" argument must be of type string. Received undefined #399

Closed
douglascayers opened this issue Jan 26, 2024 · 0 comments · Fixed by #400

Comments

@douglascayers
Copy link
Contributor

douglascayers commented Jan 26, 2024

Summary

👋 Hello, thanks for this great project!

I'm converting a typescript project from CJS to ESM (😭) and I encountered an issue in one of my tests that uses code that uses electron-log.

In my original code base, I dynamically imported electron-log/main when it was needed, but with the ESM version of my project I now statically import it import electronLogMain from 'electron-log/main.js';. Not sure if that's related, just giving some context.

In any case, it seems electron isn't initialized in my new world like it had been and so electron.app.name or electron.app.getName() return undefined, which led to the following error message:

Unhandled electron-log error TypeError: The "path" argument must be of type string. Received undefined
        at new NodeError (node:internal/errors:405:5)
        at validateString (node:internal/validators:162:11)
        at Object.join (node:path:1171:7)
        at ElectronExternalApi.getAppLogPath (/path/to/app/node_modules/electron-log/src/node/NodeExternalApi.js:17:19)
        at ElectronExternalApi.getPathVariables (/path/to/app/node_modules/electron-log/src/node/NodeExternalApi.js:95:31)
        at initializeOnFirstAccess (/path/to/app/node_modules/electron-log/src/node/transports/file/index.js:84:23)
        at getFile (/path/to/app/node_modules/electron-log/src/node/transports/file/index.js:117:5)
        at transport (/path/to/app/node_modules/electron-log/src/node/transports/file/index.js:60:18)
        at Logger.processMessage (/path/to/app/node_modules/electron-log/src/core/Logger.js:173:11)
        at Logger.logData (/path/to/app/node_modules/electron-log/src/core/Logger.js:129:10)
        at Object.newScope.<computed> (/path/to/app/node_modules/electron-log/src/core/scope.js:26:42)
        at Object.<anonymous> (/path/to/app/node_modules/jest-mock/build/index.js:794:25)
        at /path/to/app/node_modules/jest-mock/build/index.js:397:39
        at Object.<anonymous> (/path/to/app/node_modules/jest-mock/build/index.js:404:13)
        at Object.mockConstructor [as info] (/path/to/app/node_modules/jest-mock/build/index.js:148:19)

Digging into the electron-log code, I identified the two methods that were impacted:

class ElectronExternalApi extends NodeExternalApi {
  getAppName() {
    try {
      return electron.app?.name || electron.app?.getName();
    } catch {
      return super.getAppName();
    }
  }

  getAppVersion() {
    try {
      return electron.app?.getVersion();
    } catch {
      return super.getAppVersion();
    }
  }

  ...
}

Note that the super fallback is only called if an error was caught, not also if electron.app.xyz was undefined.

Expected Result

  • When electron.app.name or electron.app.getName() or electron.app.getVersion() return undefined then fallback to calling the super method just as when catching an error.

Actual Result

  • Error thrown Unhandled electron-log error TypeError: The "path" argument must be of type string. Received undefined

Versions

"electron-log": "^5.1.0"
"electron": "^28.2.0"
"jest": "^29.7.0"
"typescript": "^5.3.3"

Node v18.18.0
megahertz pushed a commit that referenced this issue Feb 1, 2024
…ined, fixes #399 (#400)

* fix: ElectronExternalApi fallback to super when electron app is undefined; accept electron arg

* test: ElectronExternalApi fallbacks to super when electron app is undefined

* chore: Comsetic changes
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

Successfully merging a pull request may close this issue.

1 participant