Feat: Improved error handling and response status availability by jhildenbiddle · Pull Request #2303 · docsifyjs/docsify

Summary

  • Add response status to internal route object
  • Expose response status to plugin hooks to allow for custom error handling.
  • Update default error page content with error status and status text (e.g. "401 - Not Authorized", "500 - Internal Server Error", etc.) instead of displaying "404 - Not Found" for all errors.
  • Fix issue where initial site render was incomplete when content fetch failed (e.g., no sidebar, plugins halted, etc.)
  • Fix issue where empty markdown pages/routes were handled as 404 errors.
  • Add tests to verify default error handling
  • Add tests to verify notFoundPage error handling
  • Add tests to verify response data availability via plugins

See #2294 for details.

Screenshots

Custom error handling via plugins (example)

CleanShot 2023-11-16 at 21 57 43@2x

window.$docsify = {
  // ...
  plugins: [
    function (hook, vm) {
      hook.beforeEach(html => {
        const { file, response } = vm.route;
        const { ok, status, statusText } = response;

        if (ok === false) {
          return [
            `# Oops!`,
            `A **${status} - ${statusText}** error occurred while requesting **${vm.route.file}**`,
          ].join('\n\n');
        }
      });
    },
  ],
}

Default error content updates (500 error)

Before: Displays inaccurate error information (shows 404 but should be 500)

CleanShot 2023-11-16 at 19 03 25@2x

After: Displays accurate error information

CleanShot 2023-11-16 at 19 03 32@2x


Initial render with content load error

Before: No sidebar, plugins halted

CleanShot 2023-11-16 at 18 22 49@2x

After: Sidebar rendered, plugins complete

CleanShot 2023-11-16 at 18 22 56@2x


Rendering empty files/routes

Before: Empty files incorrectly treated as 404 errors

CleanShot 2023-11-16 at 19 10 49@2x

After: Empty files rendered properly

CleanShot 2023-11-16 at 19 11 01@2x


Related issue, if any:

#2294

What kind of change does this PR introduce?

Feature

For any code change,

  • Related tests have been added or updated, if needed

Does this PR introduce a breaking change?

No

Tested in the following browsers:

  • Chrome
  • Firefox
  • Safari
  • Edge