Skip to content

Give file sources one source interface that opens rows for a run and reports errors in one place #2677

Open
0 of 1 issue completed

Description

@mathiasrw

What happens today

FROM calls a file source through its datafn in compileFrom (src/select/from.js) as alasql.from[NAME](arg1, arg2, cb, idx, query), with queryfn2 as cb. Each file source in src/formats/from.js reads with alasql.utils.loadFile or loadBinaryFile (src/utils/fileio.js), parses, and handles errors on its own.

  • alasql.from.JSON and the jsonl factory for JSONL and NDJSON each have their own handler that calls query.cb(null, error). A bad JSONL line throws inside the load callback, outside any handler.
  • alasql.from.TXT passes no error handler to loadFile.
  • alasql.from.CSV passes e => query.cb(null, e), which fails when query is not given.
  • XLSXLSX (XLS, XLSX, ODS) has a third copy of the handler.

This also fixes #3142.

What to change

  1. Add src/engine/source.js with the interface and the one place that reports an error.

    // A source gives the rows of one run. columns is null when known only once rows arrive.
    // open returns an array (sync) or a promise of an array (async).
    // { columns: Column[] | null, open(ctx, args) }
    
    export function reportError(err, query) {
      const error = err instanceof Error ? err : new Error(err);
      if (query && query.cb) return query.cb(null, error);
      throw error;
    }
  2. Add fileSource({ext, binary, parse}) in src/formats/from.js. open(ctx, [filename, opts]) applies autoExtFilename, reads with loadFile or loadBinaryFile, runs parse(data, opts) inside a try, and resolves with the rows or rejects with the read or parse error. It reads synchronously when the caller passes no callback, as today.

  3. Write each format as a parse function only. These are parseJSON, parseJSONL (keeps the message Could not parse JSON at line N: ...), parseTXT, parseCSV (the current parseText, with TSV and TAB setting the separator) and parseSheet for XLS, XLSX and ODS (the current processSheet). alasql.from.FILE keeps choosing the format by extension.

  4. alasql.from.JSON, JSONL, NDJSON, TXT, CSV, TSV, TAB, XLS, XLSX and ODS stay callable with (filename, opts, cb, idx, query). One adapter turns a source into that function.

    function fromSource(source) {
      return function (arg, opts, cb, idx, query) {
        let rows;
        const done = r => { rows = r; if (cb) rows = cb(r, idx, query); };
        let opened;
        try {
          opened = source.open(query && query.ctx, [arg, opts], !!cb);
        } catch (e) {
          return reportError(e, query);
        }
        if (opened && typeof opened.then === 'function') opened.then(done, e => reportError(e, query));
        else done(opened);
        return rows;
      };
    }

    Do not catch an error thrown by cb here. That is Run a statement through one runner so every error reaches the caller, and key the statement cache on every compile option #2652.

Only file sources move in this pull request.

What stays the same

Done when

  • A new test file named after this issue checks JSON, JSONL, NDJSON, TXT, CSV, TSV and XLSX. alasql.promise and the callback form reject with the error for a missing file and for content that cannot be parsed, and give today's rows for a good file. Each case has a time limit, so a promise that never settles fails the test.
  • src/formats/from.js has no error handler of its own left. Every file source goes through reportError.
  • The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.

Fixes

Fixes #3142.

Depends on

#2667

Sub-issues

Sub-issues

0 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. added this to the v4.x Focus milestone on Nov 26, 2026
  2. changed the title [-]Review #2082 for code improvements: each file source handles its own errors[/-] [+]Give file sources one source interface that opens rows for a run and reports errors in one place[/+] on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions