ponder-sh / ponder

A backend framework for crypto apps
https://ponder.sh
MIT License
597 stars 87 forks source link

[Bug] SQLite error parsing issue #1098

Open danconnell opened 6 days ago

danconnell commented 6 days ago

Seeing TypeError: parameters.reduce is not a function when a SQLite error occurs.

Coming from this code

          let parameters = (args[0] ?? []) as string[];
          parameters =
            parameters.length <= 25
              ? parameters
              : parameters.slice(0, 26).concat(["..."]);
          const params = parameters.reduce<Record<number, any>>(
            (acc, parameter, idx) => {
              acc[idx + 1] = parameter;
              return acc;
            },
            {},
          );

located at https://github.com/ponder-sh/ponder/blob/main/packages/core/src/utils/sqlite.ts#L40

This is happening because parameters is a string and represents the first arg that was passed to the query as this code is currently written.

I'm not sure what exactly the intention is with this code, but perhaps it should be something more along the lines of this

          const params = (args ?? []).reduce<Record<number, any>>(
            (acc, parameter, idx) => {
              acc[idx + 1] = parameter.length <= 25
              ? parameter
              : parameter.slice(0, 26).concat(["..."]);
              return acc;
            },
            {},
          );

As it is right now, when a SQLite error occurs it's impossible to see the actual error that did occur because of this TypeError happening. So perhaps also worth adding a try/catch around this error parsing as well.

In case it's relevant, the error I was receiving was a integer overflow error from using sum on a bigint column with negative values and the EVM_MIN_INT encoding that is done by this repo causing that sum operation to overflow

0xOlias commented 6 days ago

Yea, this error parsing code is a bit cursed - thanks for flagging. Will work on a reproduction + fix.