Skip to content

[API] Error not reported in callback #555

@regseb

Description

@regseb

Have you checked that the bug is not already fixed?

Yes

Summary

For the API, when a URL is invalid: the problem isn't reported in the callback of markdownLinkCheck().

Affected version

3.14.2

Steps to reproduce the bug

import markdownLinkCheck from "markdown-link-check";

try {
    markdownLinkCheck("[foo](http//bar)", {}, (err, results) => {
        console.log("CALLBACK", err, results);
    });
} catch (err) {
    console.log("CATCH", err);
}

Expected behavior

CALLBACK TypeError: Invalid URL
    at ...

Actual behavior

CATCH TypeError: Invalid URL
    at new URL (node:internal/url:828:25)
    at linkCheck (/home/regseb/testcase/node_modules/link-check/index.js:21:47)
    at /home/regseb/testcase/node_modules/markdown-link-check/index.js:203:9
    at /home/regseb/testcase/node_modules/async/dist/async.js:247:13
    at replenish (/home/regseb/testcase/node_modules/async/dist/async.js:447:21)
    at /home/regseb/testcase/node_modules/async/dist/async.js:452:13
    at _asyncMap (/home/regseb/testcase/node_modules/async/dist/async.js:245:16)
    at Object.mapLimit (/home/regseb/testcase/node_modules/async/dist/async.js:2092:16)
    at Object.awaitable [as mapLimit] (/home/regseb/testcase/node_modules/async/dist/async.js:212:32)
    at markdownLinkCheck (/home/regseb/testcase/node_modules/markdown-link-check/index.js:142:11) {
  code: 'ERR_INVALID_URL',
  input: 'http//bar'
}

Additional context

If the invalid URL is the third (or more) URL in the file, the error cannot be caught. This is because the function that checks the URL is executed asynchronously, and therefore the program has already exited the try-catch block.

import markdownLinkCheck from "markdown-link-check";

const markdown =
    "[foo](https://example.com/)\n" +
    "[bar](https://www.iana.org/help/example-domains)\n" +
    "[baz](http//qux)\n";
try {
    markdownLinkCheck(markdown, {}, (err, results) => {
        console.log("CALLBACK", err, results);
    });
} catch (err) {
    console.log("CATCH", err);
}
node:internal/url:828
      href = bindingUrl.parse(input, base, true);
                        ^

TypeError: Invalid URL
    at new URL (node:internal/url:828:25)
    at linkCheck (/home/regseb/regseb/testcase/node_modules/link-check/index.js:21:47)
    at /home/regseb/regseb/testcase/node_modules/markdown-link-check/index.js:203:9
    at /home/regseb/regseb/testcase/node_modules/async/dist/async.js:247:13
    at replenish (/home/regseb/regseb/testcase/node_modules/async/dist/async.js:447:21)
    at iterateeCallback (/home/regseb/regseb/testcase/node_modules/async/dist/async.js:431:21)
    at /home/regseb/regseb/testcase/node_modules/async/dist/async.js:328:20
    at /home/regseb/regseb/testcase/node_modules/async/dist/async.js:249:17
    at /home/regseb/regseb/testcase/node_modules/markdown-link-check/index.js:219:13
    at /home/regseb/regseb/testcase/node_modules/link-check/lib/proto/http.js:63:17 {
  code: 'ERR_INVALID_URL',
  input: 'http//qux'
}

Node.js v24.13.0

Can you fix it?

Maybe

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugissue is a bug or pull request fixes a bugtriage

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions