Skip to main content

Goerr

Go style explicit error handling in Python. Features:

  • Pretty print of error details
  • Trace errors across the call stack
  • Log errors
  • Test errors
pip install goerr

Documentation

API

Class Err

Methods:

err: creates a new error, print it and store it in the trace if the option is activated. Exit the program if the trace is not activated. Parameters:

  • ex: an exception (optional)
  • msg: the message string (optional) Either a message string or an exception has to be provided as argument for the error to be printed. If no argument is provided it will just record the function name to keep a trace of the call stack

warning: prints a warning message

info: prints an info message

debug: prints a debug message

panic: force program exit after an error even if the errors trace is activated

to_dict: returns a dictionnary with the error details

Class Log

Same as Err but log errors

Properties

log_path: path of the file where to log. Default is "errors.log"
log_format: csv or text. Default is "csv". Note: the tracebacks are not recorded if the format is csv.

Class Trace

Same as Err but trace errors

Properties

trace: prints the errors trace and reset it via: same as err with an empty error: to record the call stack. See example

Example

Check the examples directory for code

Trace errors across the call stack:

import time
from goerr import Trace

err = Trace()

def func1():
    print("Func 1 running")
    time.sleep(0.5)
    try:
        'x' > 1
    except Exception as e:
        err.new("Errmsg frun func1", e)
    print("Func 1 finished")

def func2():
    func1()
    time.sleep(0.5)
    err.via("Errmsg frun func2")
    print("Func 2 running")
       
def func3():
    func2()
    time.sleep(0.5)
    err.via()
    print("Func 3 running")
        
func3()
err.trace()

Output:

Stack trace screenshot

Testing errors in programs

A helper function is available to test errors:

testing.assert_err: parameters:

  • error type: a string with the error type
  • function to run: the function to test
  • *args: function arguments
  • **kwargs: function keyword arguments

Example:

# the program
from goerr import Err
   
   class Foo(Err):
      def func1(self, param1, param2):
          try:
              param1 > param2
          except Exception as e:
              self.err(e)

# the test
import unittest
from goerr.testing import assert_err
from myprogram import Foo

   class MyTest(unittest.TestCase):
	  def test_myprogram(self):
          foo = Foo()
          assert_err("TypeError", foo.func1, 1, "bar")

Why?

I like the explicit errors management in Go (unlike many people) and I wanted to have the same kind of experience in Python: a fined grained control over errors all across the call stack.

The same lib in Go: terr

Metadata

Release files for goerr 0.11.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for goerr 0.11.0
File Size Uploaded
goerr-0.11.0.tar.gz 9.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for goerr 0.11.0
File Interpreter ABI Platform
goerr-0.11.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.5 kB

Release files / goerr-0.11.0.tar.gz

Download URL goerr-0.11.0.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e72443f4f07927c71053d2505223219cc15957b8ce5fd0633bdad8eba99c6a5e
BLAKE2b-256 checksum
How to use checksums
d6bda391c0b582a29b04a6a6d92f79d760835e0dfebe5600fd48d04b421f5db4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.10

Release files / goerr-0.11.0-py3-none-any.whl

Download URL goerr-0.11.0-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a67622585c2d862b266c6e354fae3831e9a566fc0c78647df879005324b3d17c
BLAKE2b-256 checksum
How to use checksums
aeeb320ebca122649ef9e41a0bfd625f633e5d49a568cb9fedecf94f370d4173
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.11.0 This release

2 release files

0.9.5

1 release file

0.9.4

1 release file

0.9.3

1 release file

0.9.2

1 release file

0.9.1

1 release file

0.9

1 release file

0.8.2

1 release file

0.8.1

1 release file

0.8

1 release file

0.7.4

1 release file

0.7.3

1 release file

0.7.2

1 release file

0.7.1

1 release file

0.7

1 release file

0.6

1 release file

0.5

1 release file

0.4

1 release file

0.3

1 release file

0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page