Best Practices

Error Handling & Log Management

From Glitches to Growth: Navigating Error Handling and Logs
  • LinkedIn
  • YouTube
  • Spotify

What is Error handling

Refers to the response and recovery procedures from error conditions present in a software application. In other words, it is the process comprised of anticipation, detection and resolution of application errors, programming errors or communication errors. Error handling helps in maintaining the normal flow of program execution. (https://www.techopedia.com/definition/16626/error-handling)

Every system, application and infrastructure must have a way of handling errors, If a user goes to an application and the unexpected happen this will cause a frustration to the user what is going on, the user may end up like this guy

Story Image

How do we prevent such things from happening you may ask, well here are some of the things we do

From Bugs to Hugs: Embracing Error Handling in API Calls

For us developers, example we have API call that gets data from a server like this getMovieDetails

Story Image

This is a simple call from IMDB API, as you can observe here inside the function we have a try & and catch block. The try statement allows a block of code to run/tested while it’s executing. If the code inside the try block succeeds, this will return the data from the server.

As for the catch statement, this block runs when an error occurs. In this section, you can throw the error object inside the block, but wait! You can also customize your error rather than relying on its error object and give a more meaningful error for the user, and we have a block that can be extended for this try and catch it’s called finally

The finally block always runs every time the function is called either the function returns a data or an error, We can use this block for anything that we like to run like showing a component or anything else that you like.

Oops! Something Went Bonkers: Catching Errors in Style!

But catching errors like this isn’t enough, we need something to show to the user that error happens, giving a meaningful error for the user to see is very important to know what’s going on. This ensures that the application they’re visiting is reliable and user-friendly.

Story Image

Here we use a package called react-toastify, simply attached a function to the onClick event that will trigger the toast, using tools like this can make your application user-friendly to the user. For more information on this package, click here.

Story Image

Stay Ahead, Stay Secure: The Power of Log Management

Refers to the process of collecting, storing, analyzing, and monitoring logs generated by various systems, applications, and network devices within an organization's IT infrastructure. Logs are essentially records of events, activities, or messages that occur within these systems. They can include information such as user actions, system events, errors, security-related events, and other relevant data.

Logging errors can be very helpful. This will give you insights of what happened to your application. It provides valuable information that enables organizations to maintain efficient and secure systems while meeting regulatory requirements. Without proper log management, detecting issues, responding to incidents, and ensuring system integrity become challenging tasks.

Story Image

Here are some of the reasons why you should have a log management to your application:

Real-time Monitoring: Log management tools allow real-time monitoring of system logs, enabling the detection of anomalies, errors, and performance issues as they occur. This proactive approach helps identify and resolve problems before they escalate.

Issue Detection: Logs provide a detailed record of events, errors, and activities within a system. By analyzing these logs, developers and system administrators can pinpoint the root cause of issues, such as application crashes, network failures, or security breaches.

Performance Optimization: By analyzing logs, teams can identify performance bottlenecks, resource utilization problems, and inefficient processes. Optimizing these areas can lead to improved system performance and a better user experience.

Let me introduce you to a tool that you can use in your next project in the future.

Sentry, Not the superhero one

Sentry.io is a third party service to track errors and monitor performance. Plesk Obsidian uses this system to get detailed information about product errors and performance bottlenecks, which helps to fix bugs and make the product more stable and reliable

Here we set up sentry into our Next.js project, and created a button and added a function that doesn’t exist in the codebase and attached to the onClick event of the button.

Story Image

After the button is clicked, this will lead to an error and will break the application

Story Image

This error will be monitored in the issue section in our dashboard as we expand the issue, we can see the details of the said error such as things where in the code does it happen, what device is the user using, a replay of how it happened and so much more.

Story Image

Adding this third party service in your application can help you fix, improve your codebase and also saves your time as stated in their page “Working code, Happy customers

For installation setup of Sentry, click here.

Conclusion

Error handling and log management are critical aspects of software development and maintenance. Properly implementing error handling mechanisms and effective log management practices can significantly improve the reliability, maintainability, and performance of a software application. Implementing these ways to your application can make your users happy.

Story Image

About the Contributor

Discuss this topic with an expert.