Skip to content
Muslu Yüksektepe

cemir_error

A library that catches Python and PyPy errors and shows them in detail, with color.

cemir_error is a small library that prints errors caught in Python and PyPy programs in a colored, explained form. It shows the error type, what it means, how it can be fixed and the file and line in the project where it happened, all together. Explanations are ready in Turkish, English, French and German. I wrote it in October 2023 and published it on PyPI as cemir-error.

Purpose

Python's default error output, the traceback, lists every function called up to the point of the error, including the internal files of the libraries in use. Especially for Python beginners, finding the actual line in their own code within that long list, and understanding what the error message is trying to say, is hard. I made cemir_error to bring forward only the part of the error that belongs to the project and to explain the error type in plain language.

How it works

The library's main function is error_tracking. It is called in the except part of a try block with the caught error object, the sys module and the desired language code. The function first gets the name of the error type and looks up the explanation and the suggested fix for that type in the selected language's dictionary file. It then prints, in color, the date and time of the error, the error type, its explanation and the suggested fix.

Next it walks the traceback entries from last to first. It compares each entry's file path with the project path, made of the last four folders of the current working directory. It shows only entries that belong to files inside the project; for each one it prints the last part of the file path, the line number and the line of code where the error happened. This keeps entries from libraries' internal files off the screen.

Key features

  • It includes explanations and suggested fixes for 18 common error types, including division by zero, value, type, key, file not found, index, attribute, import and syntax errors.
  • It offers the explanations in four languages, and the language is chosen with a single parameter.
  • Because it shows only lines from the project's own files, the source of the error is found faster.
  • The date, error type, file path, line number and code line are set apart with different colors.

Technical stack

The library uses the traceback, os and datetime modules from the Python standard library and has no external dependencies. Each language's explanations live in a separate Python file, in a dictionary keyed by the error type name. Colors come from ANSI escape codes. The package was prepared with setuptools and is distributed under the MIT license.

Status

The library has not been updated since the releases in October 2023. When an error type that is not in the dictionary is caught, the function itself fails while looking up the explanation, so for now it can only be used safely with the error types on the list.

Use cases

  • It can help people learning Python read error messages in their own language and understand what to do next.
  • It is useful in small scripts and during development for quickly seeing where in the project an error comes from.
Category
Python libraries
Year
2023

Technologies

  • Python
  • PyPy
  • PyPI

Links

  • GitHub https://github.com/muslu/cemir_error
  • PyPI https://pypi.org/project/cemir-error/

Back to all projects