Skip to main content

Go like errors for Python

Project description

pygoerrors

A Python library that brings Go-style error handling to Python, enabling error values, wrapping, and inspection without relying on exceptions.​

🚀 Features

  • Error Values: Return errors as values instead of raising exceptions.
  • Error Wrapping: Wrap errors with contextual messages, similar to Go's fmt.Errorf("...: %w", err).
  • Error Inspection: Traverse error chains using unwrap(), is_(), and as_().
  • Protocol-Based Design: Utilize Python's Protocol to define error interfaces.
  • No Exceptions: Emphasize functional programming by avoiding exceptions entirely.​

📦 Installation

Install the package using pip:

pip install pygoerrors

🧑‍💻 Usage

Defining Custom Errors

import pygoerrors

MyOtherError = pygoerrors.new("some error")

class MyErrorClass(pygoerrors.Error):
    def error(self) -> str:
        return "some error"

Wrapping Errors

import pygoerrors

class MyErrorClass(pygoerrors.Error):
    def error(self) -> str:
        return "base error"

base_err = MyErrorClass()
assert base_err.error() == "base error"

wrapped_err = pygoerrors.errorf("wrapped: %w", base_err)
assert wrapped_err.error() == "wrapped: base error"

Inspecting Errors

import pygoerrors

class MyErrorClass(pygoerrors.Error):
    def error(self) -> str:
        return "base error"

base_err = MyErrorClass()
assert base_err.error() == "base error"

wrapped_err = pygoerrors.errorf("wrapped: %w", base_err)
assert wrapped_err.error() == "wrapped: base error"

assert pygoerrors.is_(wrapped_err, MyErrorClass())

instance = pygoerrors.as_(err, MyErrorClass)
assert instance.error() == "base error"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygoerrors-0.2.0.tar.gz (29.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pygoerrors-0.2.0-py3-none-any.whl (30.9 kB view details)

Uploaded Python 3

File details

Details for the file pygoerrors-0.2.0.tar.gz.

File metadata

  • Download URL: pygoerrors-0.2.0.tar.gz
  • Upload date:
  • Size: 29.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pygoerrors-0.2.0.tar.gz
Algorithm Hash digest
SHA256 96240980a00a0fe23af65c0b3eeb716c7f290d5a7ae156bf12ddfb7a08b2add7
MD5 532696f042489b5cb69780bc4d28f077
BLAKE2b-256 9f0df7ece90a9355044a5c0110fbf473bb74e226dd068b7710a937d9fbe49768

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygoerrors-0.2.0.tar.gz:

Publisher: release.yaml on dreadster3/pygoerrors

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pygoerrors-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pygoerrors-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 30.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pygoerrors-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe778fdbbde2140f666e7c2ed94da39a3c55b18f9f65aa732cf872b3e83689bd
MD5 ce2fee43e618cc68fdc8791f87d8d6e2
BLAKE2b-256 88360510a69240ea3409a7e26364ce3cea377e26312b0ad5c664f9fb6c3c198d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygoerrors-0.2.0-py3-none-any.whl:

Publisher: release.yaml on dreadster3/pygoerrors

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page