5 Best Practices in Handling HTTP Errors in JavaScript
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (179).NET Core  (29).NET MAUI  (214)Angular  (110)ASP.NET  (51)ASP.NET Core  (83)ASP.NET MVC  (90)Azure  (41)Black Friday Deal  (1)Blazor  (226)BoldSign  (15)DocIO  (24)Essential JS 2  (109)Essential Studio  (200)File Formats  (68)Flutter  (133)JavaScript  (226)Microsoft  (120)PDF  (81)Python  (1)React  (105)Streamlit  (1)Succinctly series  (131)Syncfusion  (942)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (52)Windows Forms  (61)WinUI  (71)WPF  (163)Xamarin  (161)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (9)Business intelligence  (55)Button  (4)C#  (157)Chart  (140)Chart of the week  (54)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (70)Development  (658)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (14)Excel  (43)Extensions  (22)File Manager  (7)Gantt  (19)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (512)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (4)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (14)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (393)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (614)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
5 Best Practices in Handling HTTP Errors in JavaScript

5 Best Practices in Handling HTTP Errors in JavaScript

Errors are an everyday obstacle that all developers and clients face. A typical error is when applications or a particular function breaks due to its inability to handle the logic or parameters.

JavaScript errors are similar to those found in other languages, as they follow the same principles. Following best practices for error handling ensures that the components will not break when the application runs into an error.

This article covers some best practices in handling HTTP errors within a JavaScript application.

Types of Errors: Operational and Programmer Errors

Most errors within applications are due to mistakes made by developers in their development processes. However, this is not the case for all errors.

These errors fall into two categories: operational and programmer.

Explore the best and most comprehensive JavaScript UI controls library in the market.

Operational Errors

These are the type of errors seen during the application’s runtime. It does not mean there are bugs within the application, however. These errors come from within the JavaScript runtime environment.

Some of the most common operational errors include:

  • Server returning HTTP 500 response.
  • Request timing out.
  • Failing to resolve the hostname.

Programmer Errors

Programmer errors are issues within the application’s code, most likely introduced by the developers when building the application. Therefore, these errors are bugs within the application, and efficient remediation is a must to avoid issues.

Some of the most common programmer errors include:

  • Not catching a rejected promise.
  • Passing a string variable when the input expects an object.
  • Passing incorrect parameters into a function.

Best Practices for Handling HTTP Errors in JavaScript

Even though these are two different types of errors, they can both cause issues that affect the application’s performance.

All errors within the application must be dealt with appropriately. Adhering to best practices is a good start.

The following best practices cover the most common issues with error handling in JavaScript.

Everything a developer needs to know to use JavaScript control in the web app is completely documented.

Specific Error Messages

Taking shortcuts may tempt developers into including generic error messages within the application so that multiple errors have the same output. Since the developer can reuse the same error message and code to handle most errors thrown by the application or a specific function, this method saves time.

However, having generic error messages within an application makes it less user-friendly, as understanding or troubleshooting them becomes tedious for users.

Best practice says that different errors must have individual and unique error messages displayed to the user. This approach allows users to understand and troubleshoot errors efficiently.

For example, developers may use libraries to initiate HTTP requests. The following code shows how developers can handle HTTP errors within Axios.

axios.get('/user/test').catch(function (error) {
 if (error.response) {
  // The request was made and the server responded with a status code that falls out of the range of 2xx
  console.log(error.response.data);
  console.log(error.response.status);
  console.log(error.response.headers);
  if (error.response.status === 500){
    //Custom Error Handler
    handlehttp500();
  }
  else if (error.response.status === 404){
    //Custom Error Handler
    handlehttp404();
  }
  else {
    handlehttpgenericerror();
  }
 }
});

Use Custom Error Classes to Define Specific Errors

Handling errors using native classes would provide an easy way around handling errors within an application. However, this brings in added complexity and makes the code less readable.

The use of custom error classes is a workaround for this issue. Custom error classes bring in more readability and customization to define specific errors within the application.

The following code snippet shows an example of this scenario extended for handling an HTTP 404 error.

class ResourceNotFoundError extends Error {
  constructor(code, message) {
    super(message);
    this.code = code;
    this.name = "ResourceNotFoundError";
  }
}

const doSomething = () => {
  try {
    throw new ResourceNotFoundError(404, "Resource not found");
  } catch (err) {
    if (err instanceof ResourceNotFoundError) {
      // error instance of ResourceNotFoundError
      console.log(err.code);
      console.log(err.message);
      return;
    }
    console.log(err);
  }
};

doSomething();

The replication of the same methods can let you reuse the same error class within the application, thus not requiring the developer to define the error classes repeatedly within the different classes.

To make it easy for developers to include Syncfusion JavaScript controls in their projects, we have shared some working ones.

Use Middleware

As an application grows in size and complexity, managing errors within the code becomes exhausting. An easy solution to this issue is using middleware to configure centralized error handling within the application.

Middleware provides added versatility in defining what happens when an error occurs. Developers can define the necessary actions for each error and where to send them by configuring notifications via alert notifications.

A centrally managed error handling mechanism reduces the risk of accidentally exposing errors to application users, since all errors have a single management point.

Use Promise-Based Error Handling Instead of Callbacks

Using callbacks for handling errors has been the go-to method for a long time. However, this method makes the code unreadable when the functions within the callbacks keep expanding.

In this example, the code requires multiple catch clauses to handle all the errors thrown from within the nested callback functions.

const fetchUsersCallback = () => {  
   // random url  
  const URL = '';  
  fetch(URL).then((success) => {  
    success.json().then((loggedinuser) => {  
      // do something with the data  
    }).catch((err) => {  
      console.log(err);  
    })  
  }).catch((error) => {  
    console.log(error);  
  });  
}

However, using promise-based error handling, the same section of code looks cleaner and much more readable, reducing the number of lines required to achieve the same functionality.

const fetchUsersPromise = async () => {
    // random urlconst URL = '';
    try {   
      const resp = await fetch(URL);
      const parsedResp = await resp.json();
    } catch (err) {
        console.log(err)
    }
}

It also does not require multiple instances to throw the error from this section and instead only defines a single catch clause to conduct this function.

Syncfusion JavaScript controls allow you to build powerful line-of-business applications.

Using a Centralized Location for Logs and Error Alerting

Handling errors is only a part of the solution when it comes to error management within complex applications. Even though the application implements proper error management, developers must manually dig through individual stack traces to identify the issues. This method is immensely time-consuming and ineffective in proactively identifying issues.

This is where a centralized location for logs comes in to allow all the errors and logs to reside within a single location for easy access and management. Developers may consider adding a centralized log management solution such as Elasticsearch to collect and aggregate all the errors that the application throws.

Dedicated solutions such as Elasticsearch allow developers to use advanced analytics to identify patterns, drill down into specific errors, and correlate them with other logs and metrics to provide improved insights into the collected errors.

Apart from collecting errors in a centralized location, developers may also use alerting functionality to make detecting specific errors easier within complicated applications.

Easily build real-time apps with Syncfusion’s high-performance, lightweight, modular, and responsive JavaScript UI components.

Key Takeaways

In this article, I have discussed five of the best practices that allow developers to be on top of managing HTTP errors within JavaScript applications. These best practices lay the foundation for good error-handling.

I hope you have found this helpful. Thank you for reading!

The Syncfusion JavaScript suite is the only suite you will ever need to build an application. It contains over 65 high-performance, lightweight, modular, and responsive UI components in a single package. Download the free trial and evaluate the controls today.

If you have any questions or comments, you can contact us through our support forumssupport portal, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed