Skip to main content

My friendly error base class - for Python.

Project description

mybad PyPI version Build Status Coverage Status

My friendly error base class - for Python.

Introduction

One in general always needs a application/library specific error base class, but the native errors are very limited in what meta/debugging information they can hold at time they are raised. For better debugging and error reporting/inspection this error base class allows to attach some additonal error context information that can be used to better understand the issue - without having to create custom error formatters, or run debugger.

Install

Install using pip:

$ pip install mybad

Use

Very basic example:

from mybad import Error

class ToMuchError(Error):
    pass

def print_money(stash):
    try:
        if isinstance(stash, str) and len(stash) > 13:
            raise Exception('Too much money to print: {0}'.format(stash))

        print('PRINT {0}'.format(stash))

    except Exception as error:
        raise ToMuchError(error,
            message = 'Out of money printing ink...',
            id = hash(stash),
            key = 'too_much',
            code = 400,
            details = dict(
                stash = stash,
            )
        )

amount = ''

for dollar in range(42):
    amount += '$'

    print_money(amount)

Run this with optional environment variables COLORS / ERROR_COLORS and/or VERBOSE / ERROR_VERBOSE set too truthy or falsy values, so see various error info formatting in terminal.

Something like this (imagine some colorized formatting):

PRINT $
PRINT $$
PRINT $$$
PRINT $$$$
PRINT $$$$$
PRINT $$$$$$
PRINT $$$$$$$
PRINT $$$$$$$$
PRINT $$$$$$$$$
PRINT $$$$$$$$$$
PRINT $$$$$$$$$$$
PRINT $$$$$$$$$$$$
PRINT $$$$$$$$$$$$$
===============================
     str(error)
---------------------------
Out of money printing ink... - {'stash': '$$$$$$$$$$$$$$'}

===============================
     error.stack
---------------------------
Traceback (most recent call last):
  File "examples/basic.py", line 27, in print_money
    raise Exception('Too much money to print: {0}'.format(stash))
Exception: Too much money to print: $$$$$$$$$$$$$$

===============================
     error.inspect()
---------------------------
{   'code': 400,
    'details': {'stash': '$$$$$$$$$$$$$$'},
    'id': 3563898309523127190,
    'key': 'too_much',
    'message': 'Out of money printing ink...',
    'stack': [   {   'code': ['stash = stash,'],
                     'file': 'examples/basic.py',
                     'function': 'print_money',
                     'line': 38},
                 {   'code': ['print_money(amount)'],
                     'file': 'examples/basic.py',
                     'function': '<module>',
                     'line': 48}],
    'type': 'ToMuchError'}


===============================
     error.json()
---------------------------
{
    "type": "ToMuchError",
    "id": 3563898309523127190,
    "code": 400,
    "key": "too_much",
    "message": "Out of money printing ink...",
    "details": {
        "stash": "$$$$$$$$$$$$$$"
    },
    "stack": [
        {
            "file": "examples/basic.py",
            "function": "print_money",
            "line": 38,
            "code": [
                "stash = stash,"
            ]
        },
        {
            "file": "examples/basic.py",
            "function": "<module>",
            "line": 48,
            "code": [
                "print_money(amount)"
            ]
        }
    ]
}

Test

Clone down source code:

$ make install

Run colorful tests, with only native environment (dependency sandboxing up to you):

$ make test

Run less colorful tests, with multi-environment (using tox):

$ make test-tox

Related

  • mybase - "My friendly library base class - for Python"

About

This project was mainly initiated - in lack of solid existing alternatives - to be used at our work at Markable.ai to have common code conventions between various programming environments where Python (research, CV, AI) is heavily used.

License

Released under the MIT license.

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

mybad-0.2.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

mybad-0.2.0-py2-none-any.whl (13.7 kB view details)

Uploaded Python 2

File details

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

File metadata

  • Download URL: mybad-0.2.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.13

File hashes

Hashes for mybad-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b49b155adabceb2659dbb0bfd3420671dee5febe10bf5e8d499d750d34f75616
MD5 3e57758dc33d5018a94d2657a2321b93
BLAKE2b-256 fb625366bda200b009452f4446ade7758f0a18be21a3131ca5e541d4b07e6d4e

See more details on using hashes here.

File details

Details for the file mybad-0.2.0-py2-none-any.whl.

File metadata

  • Download URL: mybad-0.2.0-py2-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.13

File hashes

Hashes for mybad-0.2.0-py2-none-any.whl
Algorithm Hash digest
SHA256 d2c8f1f9e0f9b687ab5a46689e5928314bc53ede8122611ef36442ca4c8a1bf8
MD5 83037b1fec6d987453f4bbe660bbd0aa
BLAKE2b-256 f0918267d5ba58fd4486e4c6af1ce94f15b6b0aa2988386e1067e21f8cf12c4e

See more details on using hashes here.

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