DEV Community

KiminLee
KiminLee

Posted on

fastdocs: Application crashes at invalid endpoint

What is fastdocs?

Automatically generated Docsify flavored documentation sites for markdown files! This is a README-as-a-Service project, spin up beautiful versions of your documentation in seconds!

how to use:
To render documentation from https://github.com/cheeriojs/cheerio, where the :user is cheeriojs and :repo is cheerio you can simply go to https://fastdocs.io/cheeriojs/cheerio to view the rendered documentation!

Getting start

This week, I just wandering around on the github to find interesting repo. All of sudden, I came across with fastdoc project. aemmadi is looking for help to fix the endpoint timeout bug. So I let him know I would like to work on this.

Issue: Application crashes at invalid endpoint

If the :user/:repo is invalid, the application times out and crashes
The issue seemed simple, at the very first time, I guessed "the bug is from somewhere like request. So I started to dig in there first.

Step 1: find out what is the problem

Unfortunately, this program doesn't support unit test feature, so I need to manually test it first. I passed some wrong user name and repo name like /cheeriojs -> cherrio, /cheerio -> cheerr.

After, testing couple time I ended up finding where the problem coming from

app.get("/:user/:repo", (req, res) => {
  const user = req.params.user;
  const repo = req.params.repo;

  util.compileDocs(user, repo).then(() => {
    util.serveDocs(app, user, repo);
    res.redirect(`/docs/${user}/${repo}/`);
  });
});
Enter fullscreen mode Exit fullscreen mode

In the app.get(), util.compileDocs(user, repo) crashed when invalid user and repo. I guessed there should be API request inside of the util.compileDocs(user, repo) and the request
would be something wrong.

Step 2: fix axios request

In the util.js file, compileDocs run getReadme

async function compileDocs(user, repo) {
  await getReadme(user, repo);
}
Enter fullscreen mode Exit fullscreen mode

the getReadme looked like this,

async function getReadme(user, repo) {
  mainConfig.user = user;
  mainConfig.repo = repo;

  const readme = await axios.get(
    `https://api.github.com/repos/${user}/${repo}/readme`
  );
Enter fullscreen mode Exit fullscreen mode

I found out that axios was throwing error when user and repo are invalid, but there is no try catch block. So I simply added try and catch block.

After changing, now there is no error, instead just default 404 error message cannot get :/user/repo. But the bug was only about back-end feature, so I thought it was kind of finished.

See the changes

I tested a lot of times, then finally decided it was good to go. I made a PR request for him

New request! -- add 404 page

Couple days later, he sent me a message that he would like for me to add front-end feature too. He wants to get benefit from docsify 404 error page.

Added a customized 404 page

After reviewing the documentation, I finally understand how to add this. In the HTML file added this:

window.$docsify = {
  notFoundPage: 'my404.md',
};
Enter fullscreen mode Exit fullscreen mode

then, make a 'my404.md' in docs directory.

And I changed the app.get to fit the changes

if(user && repo){
      res.redirect(`/docs/${user}/${repo}/`);
    }else{
      res.sendFile(path.resolve(__dirname, './docs/index.html'));
    }
Enter fullscreen mode Exit fullscreen mode

However, it didn't work at all. It just kept showing the default cannot get message. For some reason, this change was ignored.

The problem

Spending some hours, I figured out what the problem was. This is not because of my change, but it came from the way it was coded before. Whenever user change and get request url, it just redirect to the url

util.compileDocs(user, repo).then(() => {
    ...
    res.redirect(`/docs/${user}/${repo}/`); // the problem
    ...
  });
Enter fullscreen mode Exit fullscreen mode

Also, res.sendFile(path.resolve(__dirname,'./docs/index.html')); just send a html file, so it would not run

window.$docsify = {
  notFoundPage: 'my404.md',
};
Enter fullscreen mode Exit fullscreen mode

in the html file.

So I suggest couple another way to add 404 page without docsify, and ask for some more information about docsify features, and let him know about this.

To be updated

We are still working on this. At this moment, I am waiting for this response. If there is further update, I will update the blog as well

Top comments (0)