Skip to main content

with-err

with-err is a python library that converts try-except pattern into Go-like result, err pattern. I feel result, err pattern easier to maintain in large projects.

Getting Started

Install

pip install with-err

(with uv)

uv pip install with-err

Use as Function

import json
from with_err import with_err

json_loads_e = with_err()(json.loads)

data, err = json_loads_e('{"a": 1}')
assert err is None
assert data = {"a": 1}

data, err = json_loads_e('{"a": }')
assert err is not None
assert data is None

Use as Decorator

import json
from with_err import with_err

@with_err()
def json_loads_e(a: str | bytes | bytearray):
    return json.loads(a)

data, err = json_loads_e('{"a": 1}')
assert err is None
assert data = {"a": 1}

data, err = json_loads_e('{"a": }')
assert err is not None
assert data is None

with_err with Specified Exceptions

import json
from with_err import with_err

@with_err(json.decoder.JSONDecodeError)
def json_loads_e(a: str | bytes | bytearray):
    return json.loads(a)

data, err = json_loads_e('{"a": 1}')
assert err is None
assert data = {"a": 1}

data, err = json_loads_e('{"a": }')
assert err is not None
assert data is None

Get err Traceback Stack

import json
from with_err import with_err, get_err_strs

def json_loads_e(a: str | bytes | bytearray):
    return json.loads(a)

_data, err = json_loads_e('{"a": 1}')
err_stack = get_err_strs(err)
assert err is None
assert err_stack == []
import json
import re
from with_err import with_err, get_err_strs

def json_loads_e(a: str | bytes | bytearray):
    return json.loads(a)

_data, err = json_loads_e('{"a": }')
err_stack = get_err_strs(err)
err_str = '\n'.join(err_stack)
assert err is not None
assert len(err_stack) > 0
assert re.search(r'json/__init__.py", line \d+, in loads', err_str)
assert re.search(r', line \d+, in json_loads_e', err_str)

Download files

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

Source Distribution

with_err-1.0.0.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

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

with_err-1.0.0-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file with_err-1.0.0.tar.gz.

File metadata

  • Download URL: with_err-1.0.0.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for with_err-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3c1d2ad244d337e8c992abb9a8c5daedcf2bc1c2ab24cf5e6ca9d32588f9cc60
MD5 989d5de7b88fd5806913c195b0c4026a
BLAKE2b-256 adc0ae565c198ea3ba5b22e55f3a82128e67c39f971d0cd79af1ff0cb063e0d0

See more details on using hashes here.

File details

Details for the file with_err-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: with_err-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for with_err-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bed2d6345b523aa36bdfe0e8f6cec435441541eeb3190e3819657e828e17104d
MD5 d684da21ecda20165f17107772852874
BLAKE2b-256 2fd724f463a8151245ac420a713ee14bf056c085d9f850884342dff8f745fb5f

See more details on using hashes here.

Release history Release notifications | RSS feed

1.3.0

2 files

1.2.1

2 files

1.2.0

2 files

1.1.0

2 files

This release

1.0.0 This release

2 files

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