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

TypeError: Cannot read properties of null (reading 'name') #77

Open
abcfy2 opened this issue Sep 26, 2023 · 2 comments
Open

TypeError: Cannot read properties of null (reading 'name') #77

abcfy2 opened this issue Sep 26, 2023 · 2 comments

Comments

@abcfy2
Copy link

abcfy2 commented Sep 26, 2023

Here is the sample:

async function createAttestationKeyHandler(
  this: FastifyInstance,
  request: FastifyRequest,
  reply: FastifyReply
) {
  const dbService: DbService = this.diContainer.resolve('dbService');
  const {keypairName, privateKey} = request.body as {
    keypairName: string;
    privateKey: string;
  };
  const apiKey = getApiKey(request);

  if (
    await findAttestationKeyByProjectAndName(dbService, apiKey, keypairName)
  ) {
    return reply.status(400).send({error: 'key already exists'});
  }

  let wallet;
  let imported = 0;
  if (privateKey) {
    try {
      wallet = new ethers.Wallet(privateKey);
    } catch {
      return reply.status(400).send({error: 'invalid private key'});
    }
    imported = 1;
  } else {
    let entropy: Uint8Array = crypto.randomBytes(16);
    entropy = arrayify(
      hexDataSlice(keccak256(concat([entropy, crypto.randomBytes(16)])), 0, 16)
    );
    const mnemonic = entropyToMnemonic(entropy);
    wallet = ethers.Wallet.fromMnemonic(mnemonic);
  }

  const iv = createIvByKeyName(keypairName);
  const sk = encrypt(env.KEY_FOR_DB_CRYPTO, iv, wallet.privateKey);

  const result = await createAttestationKeyRecord(
    dbService,
    apiKey,
    keypairName,
    wallet.publicKey,
    sk,
    0,
    imported
  );

  return reply.status(201).send(result);
}

And here is the error:

❯ npx eslint .

Oops! Something went wrong! :(

ESLint: 8.45.0

TypeError: Cannot read properties of null (reading 'name')
Occurred while linting /home/fengyu/projects/AlphaWallet/common-api/backend/src/handlers/attestationKeysActions.ts:93
Rule: "security-node/detect-unhandled-async-errors"
    at isTryCatchStatement (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint-plugin-security-node/lib/rules/detect-unhandled-async-errors.js:42:73)
    at FunctionDeclaration (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint-plugin-security-node/lib/rules/detect-unhandled-async-errors.js:99:29)
    at ruleErrorHandler (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint/lib/linter/linter.js:1050:28)
    at /home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint/lib/linter/safe-emitter.js:45:58
    at Array.forEach (<anonymous>)
    at Object.emit (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint/lib/linter/node-event-generator.js:297:26)
    at NodeEventGenerator.applySelectors (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint/lib/linter/node-event-generator.js:326:22)
    at NodeEventGenerator.enterNode (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint/lib/linter/node-event-generator.js:340:14)
    at CodePathAnalyzer.enterNode (/home/fengyu/projects/AlphaWallet/common-api/backend/node_modules/eslint/lib/linter/code-path-analysis/code-path-analyzer.js:795:23)
@weyert
Copy link
Contributor

weyert commented Dec 4, 2023

You to change the try..catch to } catch (err: unknown) { to get rid of the error

@yatki
Copy link

yatki commented May 10, 2024

I am having the same issue with following syntax. In the code below, initServer type signature is (method) mybutton.initServer(): Promise<void | Error>. So we know for sure if the err is defined it's an Error object but still getting the same issue.

The current work around is to create a new Error instance.

mybutton.initServer().then((err) => {
      if (err) {
        /**
         * Calling this.myEvents.emit('error', err)
         * resulting eslint "security-node/detect-unhandled-event-errors" rule
         * to crash and throw an error. Seems like this is a bug in eslint rule.
         *
         * */
        this.myEvents.emit('error', new Error(err.message))
      }
    })

I would expect passing err object to work just fine.

Could you please take a look 🙏🏻 I can help to fix this once I have time as well.

Cheers 🖖🏻

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

3 participants