Category Archives: Web Tools

Building Resilient Applications: Python Error Handling Strategies

From “Oops” to “Oh Yeah!”: Building Resilient, User-Friendly Python Code

Errors are inevitable in any programming language, and Python is no exception. However, mastering how to anticipate, manage, and recover from these errors gracefully is what distinguishes a robust application from one that crashes unexpectedly.

In this comprehensive guide, we’ll journey through the levels of error handling in Python, equipping you with the skills to build code that not only works but works well, even when things go wrong.

Why Bother with Error Handling?

Think of your Python scripts like a well-trained pet. Without proper training (error handling), they might misbehave when faced with unexpected situations, leaving you (and your users) scratching your heads.

Well-handled errors lead to:

  • Stability: Your program doesn’t crash unexpectedly.
  • Better User Experience: Clear error messages guide users on how to fix issues.
  • Easier Debugging: Pinpoint problems faster when you know what went wrong.
  • Maintainability: Cleaner code makes it easier to make updates and changes.

Level 1: The Basics (try...except)

The cornerstone of Python error handling is the try...except block. It’s like putting your code in a safety bubble, protecting it from unexpected mishaps.

try:
    result = 10 / 0  
except ZeroDivisionError:
    print("Division by zero is not allowed.")
  • try: Enclose the code you suspect might raise an exception.
  • except: Specify the type of error you’re catching and provide a way to handle it.

Example:

try:
   num1 = int(input("Enter a number: "))
   num2 = int(input("Enter another number: "))
   result = num1 / num2
   print(f"The result of {num1} / {num2} is {result}")
except ZeroDivisionError:
   print("You can't divide by zero!")
except ValueError:
   print("Invalid input. Please enter numbers only.")

Level 2: Specific Errors, Better Messages

Python offers a wide array of built-in exceptions. Catching specific exceptions lets you tailor your error messages.

try:
  with open("nonexistent_file.txt") as file:
    contents = file.read()
except FileNotFoundError as e:
    print(f"The file you requested was not found: {e}")

Common Exceptions:

  • IndexError, KeyError, TypeError, ValueError
  • ImportError, AttributeError
try:
   # Some code that might raise multiple exceptions
except (FileNotFoundError, ZeroDivisionError) as e:
   # Handle both errors
   print(f"An error occurred: {e}")

Level 3: Raising Your Own Exceptions
Use the raise keyword to signal unexpected events in your program.

def validate_age(age):
    if age < 0:
        raise ValueError("Age cannot be negative")

Custom Exceptions:

class InvalidAgeError(ValueError):
    pass

def validate_age(age):
    if age < 0:
        raise InvalidAgeError("Age cannot be negative")

Level 4: Advanced Error Handling Techniques
Exception Chaining (raise…from): Unraveling the Root Cause


Exception chaining provides a powerful way to trace the origins of errors. In complex systems, one error often triggers another. By chaining exceptions together, you can see the full sequence of events that led to the final error, making debugging much easier.

try:
    num1 = int(input("Enter a number: "))
    num2 = int(input("Enter another number: "))
    result = num1 / num2
except ZeroDivisionError as zero_err:
    try:
        # Attempt a recovery operation (e.g., get a new denominator)
        new_num2 = int(input("Please enter a non-zero denominator: "))
        result = num1 / new_num2
    except ValueError as value_err:
        raise ValueError("Invalid input for denominator") from value_err
    except Exception as e:  # Catch any other unexpected exceptions
        raise RuntimeError("An unexpected error occurred during recovery") from e
    else:
        print(f"The result after recovery is: {result}")
finally:
    # Always close any open resources here
    pass 

Nested try…except Blocks: Handling Errors Within Error Handlers
In some cases, you might need to handle errors that occur within your error handling code. This is where nested try…except blocks come in handy:

try:
    # Code that might cause an error
except SomeException as e1:
    try:
        # Code to handle the first exception, which might itself raise an error
    except AnotherException as e2:
        # Code to handle the second exception

In this structure, the inner try…except block handles exceptions that might arise during the handling of the outer exception. This allows you to create a hierarchy of error handling, ensuring that errors are addressed at the appropriate level.


Custom Exception Classes: Tailoring Exceptions to Your Needs


Python provides a wide range of built-in exceptions, but sometimes you need to create custom exceptions that are specific to your application’s logic. This can help you provide more meaningful error messages and handle errors more effectively.

class InvalidEmailError(Exception):
    def __init__(self, email):
        self.email = email
        super().__init__(f"Invalid email address: {email}")

In this example, we’ve defined a custom exception class called InvalidEmailError that inherits from the base Exception class. This new exception class can be used to specifically signal errors related to invalid email addresses:

def send_email(email, message):
    if not is_valid_email(email):
        raise InvalidEmailError(email)
    # ... send the email

Logging Errors: Keeping a Record
Use the logging module to record details about errors for later analysis.

import logging

try:
    # Some code that might cause an error
except Exception as e:
    logging.exception("An error occurred")

Tips for Advanced Error Handling

  • Use the Right Tool for the Job: Choose the error handling technique that best fits the situation. Exception chaining is great for complex errors, while nested try...except blocks can handle errors within error handlers.
  • Document Your Error Handling: Provide clear documentation (e.g., comments, docstrings) explaining why specific exceptions are being raised or caught, and how they are handled.
  • Think Defensively: Anticipate potential errors and write code that can gracefully handle them.
  • Prioritize User Experience: Strive to provide clear, informative error messages that guide users on how to fix problems.

John

Tools Rundown: IT-Tools Docker Image!

IT-Tools docker container is a very large suite of one-off tools and utilities that you access via a web interface. It is very easy to get running in Docker via Portainer and it is also very easy to use. I see this as a utility tool for all types of people in the IT field from admins to programmers as it really covers the gamut of tools that it provides.

How to get it going in Docker on my *nix system (this works for Synology as well)

Using Compose here is the basic gist of getting it up and running in Portainer. In Portainer, add a new stack, name it what you will, and then in the editor, paste the following:

version: '3.9'
services:
    it-tools:
        image: 'corentinth/it-tools:latest'
        restart: always
        ports:
            - '5545:80'
        container_name: IT-Tools

Then click on the “Deploy Stack” button and let it do its work. You should get a message that the stack was deployed successfully once it is finished.

I honestly have no idea how to do this in Windows as that demon child of an implementation of Docker is just weird and hard to understand versus the *nix versions.

Accessing IT-Tools

Once the stack is up and running, open your browser and navigate to: http://<ipaddressofdockerhost>:5545

This should open up this page for you.

And that is all there is to it! Just click an option to open it and use it, it’s all web-based. There is literally something that everyone can use quite often in their trade I believe and it definitely worth the 10-15 minutes it takes to get it going. Just bookmark it in your browser and then you have a great go-to tool for those things that you need a converter or other utility for.

You can choose a light or dark mode, as you can see from the screenshot I have it in dark mode. You can favorite utilities and tools as well and it will pin them to the top of the page as well.

I do hope you take a few minutes and try it out. It’s just a well-thought-out app that just ticks all the marks and that is few and far between these days. You rarely come across something like this.

John

New projects: Web-Based Image Manipulators

What is it and where is it…

If you’re looking for straightforward tools to manipulate your images without the need for sophisticated software, you might want to look into a few scripts I developed. They are written in PHP and HTML5 with a lot of JS, and they are all widely used for server-side scripting. The functionality of these scripts allows users to perform basic image manipulations such as resizing and rotating images, cropping and format conversion.

Being compatible with the most common image formats like BMP, PNG, and JPG, it ensures that the largest audience can utilize its features without compatibility issues. The user interface is designed to be very easy to use, even for those who may not have extensive technical skills. This makes it suitable for anyone needing quick image adjustments without the need for detailed knowledge of image editing.

To make it accessible to everyone, I’ve hosted this script online where you can easily find it. To get started with adjusting your images, you just need to visit the following links: Resize, Crop, Convert. Here, you can upload your images and choose the desired operation – whether you want to change its size, alter its orientation, change format or whatever. These tools are learning tools and demonstrate the basics of PHP and HTML5 for simple but complex tasks. Now they may not operate the way you want but don’t abuse them or they won’t work at all. They are behind a cloudflare tunnel so there is a maximum file size limit so don’t try to convert a bunch or a large image.

Moreover, owing to their simplicity and ease of use, it’s an excellent solution for everyday image processing tasks. Whether you’re running a blog, managing a website, or even just looking to adjust some images for personal use, these PHP and HTML5 scripts aim to provide a no-fuss solution and demonstrate to you how simple things can be helpful and easy to make for one off projects. I will be uploading the code one day when I get it cleaned up and documented here: Github.com

John

Link in Bio Style Hosting Available

I’ve opened up a LinkStack server for the public that people can use instead of paying for an online service to host their Bio links for sites like Facebook or Instagram. You can sign up here for your account: https://bio.shrt.ninja

Don’t abuse it and you won’t be banned or have your account removed, just enjoy something free to use!

John