Error Handling

Most important thing in an application is to control all processes to perform the correct flow and data manipulation.

But if something goes wrong you need to know where is it and why is happening to fix it.

Managing Errors

Previous.js comes with a basic but powerfull error hangling to help you create your awesome web app with ease.

All server side errors will be logged with complete information in console.

However, sometimes we cannot give you the line number where the process is broken due to our live loading method.

start.sh

Previous server running at http://localhost:8888/
 CTRL + C to shutdown

[GET 200] 2023-09-07 18:59:11.761: /docs/errors (29ms)
[GET 200] 2023-09-07 18:59:14.790: /docs/structure (28ms)
[GET 200] 2023-09-07 18:59:18.137: /docs/errors (25ms)
[GET 404] 2023-09-07 19:04:12.947: /404 (14ms)
[GET 200] 2023-09-07 19:04:13.018: /previous.js (8ms)
[GET 200] 2023-09-07 19:04:13.028: /js/app.js (1ms)
...
[Previous App Error] ReferenceError: none is not defined
    at Object.content (eval at load (/home/user/prevapp/previous/server/load.js:24:18), <anonymous>:3:12)
    at Previous.renderRoute (/home/user/prevapp/previous/server/previous.js:230:22)
    at Previous.render (/home/user/prevapp/previous/server/previous.js:96:27)
    at IncomingMessage.<anonymous> (/home/user/prevapp/previous/index.js:110:14)
    at IncomingMessage.emit (node:events:538:35)
    at endReadableNT (node:internal/streams/readable:1345:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
[GET 500] 2023-09-07 19:13:36.328: /examples/hello?name=David (16ms)
...
[GET 200] 2023-09-07 19:16:13.572: /docs/data (29ms)
[GET 200] 2023-09-07 19:16:16.124: /docs/errors (43ms)
[GET 200] 2023-09-07 19:18:44.416: /js/app.js (5ms)
[GET 200] 2023-09-07 19:18:44.452: /img/logo-inverted.png (1ms)

Good to know

Previous.js will always try to catch errors in main loop to keep the server online, but unexpected errors in asnyc processes like promises can affect the runtime and crash your entire application.

Manually add try/catch statements inside that processes to avoid an application crash.

Learn more about capturing errors

404 Page Not Found

All non existan routes in app/ or public/ path will be considered a 404 Exception.

The server will return a plain text page with a simple message and the 404 response status header.

If you want to customize your 404 Page Not Found you can include the 404.js file in your app/ directory to generate a cute one.

return {
  title: (props) => {
    return "Not Found";
  },
  content: (props) => {
    return `
      <div class="text-center">
        <h1>404</h1>
        <h2>Page not found!</h2>
      </div>
    `;
  }
};

Good to know

This page will use your current root layout to render the content so all layout assets will be loaded within this page.

500 Server Error Page

All errors in server side will generate a 500 response status header with a plain text page with a basic information about the error.

If you want to customize your 500 Server Error Page you can include the 500.js file in your app/ directory to generate a cute one.

return {
  title: (props) => {
    return "Error";
  },
  content: ({ data }) => {
    return `
      <div class="text-center">
        <h1>500</h1>
        <h2>Server Error!</h2>
        <p>${data.error}</p>
        <pre>${data.file}</pre>
      </div>
    `;
  }
};

Use data.error to get the error message and data.file to find out which file the execution failed.

Good to know

This page will use your current root layout to render the content so all layout assets will be loaded within this page.