Skip to main content

Result Wrapper

Project description

ResultWrapper

How to install it?

You can install ResultWrapper from this Github repository with python3 setup.py install, or just install it directly from pypi with pip3 install result-wrapper.

What is it?

ResultWrapper is a very small utility library that provides a class containing either an Error, or a set of Results. This can be used in APIs to quickly and easy communicate if the operation failed or not. A ResultWrapper instance provides access to its underlying results if it did not fail, otherwise all access to possible results are denied.

How to use it?

To enable the capabilities of ResultWrapper, decorate a function with the wrap_result decorator. This function then returns an instance of ResultWrapper. To check wether a ResultWrapper is failed or not, use the .failed property.

You can pass a tuple of exception classes that should be caught and transformed to a failed Result in the decorator parameter exceptions:

from result_wrapper import wrap_result

@wrap_result(exceptions=(ValueError,))
def function1():
    raise ValueError("This exception will be caught and transformed to a failed result.")

@wrap_result(exceptions=(ValueError,))
def function2():
    raise TypeError("This exception will not be caught. The exception is thrown normally.")

assert function1().failed is True
function2() # this will throw TypeError !!!

Another way of returning a failed result is to use the ResultWrapper.make_failed function:

from result_wrapper import wrap_result, ResultWrapper

@wrap_result
def function1():
    return ResultWrapper.make_failed()

assert function1().failed is True

Optionally you can pass any object to the make_failed function to pass some error information to the caller. In the case of a thrown exception, the error information is set to the arguments to the constructor of the exception:

from result_wrapper import wrap_result, ResultWrapper

@wrap_result
def function1():
    return ResultWrapper.make_failed({"ErrorID": 1})

@wrap_result(exceptions=(ValueError,))
def function2():
    raise ValueError({"ErrorID": 1})

assert function1().error_information.get("ErrorID") == 1
assert function2().error_information.get("ErrorID") == 1

To return a successful result, return an instance of Result or ResultWrapper.make_succeeded:

from result_wrapper import wrap_result, Result, ResultWrapper

@wrap_result
def function1():
    return Result(result1="value1", result2=2)

@wrap_result
def function2():
    return ResultWrapper.make_succeeded(result1="value1", result2=2)

result = function1()
assert result.result1 == "value1"
assert result.result2 == 2

result = function2()
assert result.result1 == "value1"
assert result.result2 == 2

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

result_wrapper-1.0.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

result_wrapper-1.0.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: result_wrapper-1.0.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for result_wrapper-1.0.0.tar.gz
Algorithm Hash digest
SHA256 32ba4da39555875b36a681e90a5471398016c9443ca6d99bc7ca4e9d12d75a74
MD5 2535a698e0e2f3c163510725c08d1c46
BLAKE2b-256 bc4b9ef60d55780085439fd4ff916a1fc20147a6b39778a9a9f32fbc8358e43e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for result_wrapper-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a01d7bbb752938f57f0011c5316ee62f55a7011d93b45292be54966f612b519
MD5 b9254815aefa3195175031ef8f2ca3ae
BLAKE2b-256 8efd84ecf3ae29d2cd7d3a34c6b2730192f6c12e8a1b1d371bc94e794b325754

See more details on using hashes here.

Supported by

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