Package: unless-handler
UnlessUnlessis a lightweight python library designed to simplify error handling. It introduces theResultclass that encapsulates the result of a function call, which can be either the return value, an error, or both.Installpip3installunless-handlerUsage💡 RememberResultclass has 2 properties and 1 method,Propertiesvalue- The return value of the functionerror- Information about the error if there was anyIt can be either a tuple of the exception type and traceback string, or just the traceback stringMethodsunless- Handle errors and return thevalueImport theResultclass andtraceback.format_excfor tracebacksfromunless.resultimportResultfromtracebackimportformat_exc# OptionalIntegrateResultinto your functionInitialize theResultclass (specify the return type for type hints)defcool():to_return=Result[list]()^Returntype[list]is the return type of the function and is there so we can have type hints. It is OPTIONAL andResult()works too.Set the return valueUsevalueproperty of theResultclass to set return value# <initialized>.value = <value to return>to_return.value=[1,2,3]Catch and set errorsUseerrorproperty of theResultclass to set errorsYou have the freedom to set any value you like; however, it isrecommendedto follow the given syntax of<type>, <traceback>as it gives the ability to have caused exception type and the traceback for better error handlingtry:...exceptExceptionase:# <initialized>.error = <exception type>, <traceback.format_exc()>to_return.error=type(e),format_exc()Return the resultreturnto_returnCalling your functionSee result using thevaluepropertycalled=my_function()called.valueSee error using theerrorpropertycalled.errorOr better yet, use theunlessmethodcalled=my_function().unless()# called is now called.value and errors are handled using handler function# for more info check "Examples"ExamplesBasic usageCustom error handlerIntegrate with existing functionsBasic usagedefcool():to_return=Result[list]()try:to_return.value=[1,2,3]raiseValueError("Annoying error...")exceptExceptionase:to_return.error=type(e),traceback.format_exc()returnto_return# Calling the functionx=cool().unless()print(x)Custom error handlingYou can call functions with custom error handling logic usingResult.unlessmethod that your function returns.You can passany python functionto theunlessmethodYour handler functionmustaccept at least 1 argumentIf you're usingResult.from_funcor following the recommended syntax, the 1st argument will be a tuple consist of<type of exception>, <traceback string>Handler functioncan havekeyword arguments (x.unless(func, arg1="first", arg2="second"))defcustom_handler(e,notes):_,fmt_traceback=elogging.warn(f"{fmt_traceback}\n\nNotes:{notes}")x=cool().unless(custom_handler,notes="Probably happend because the function was hot")print(x)Integrate with existing functionsYou can useResult.from_funcmethod to integrate this with existing functions.As a decorator,@Result.from_funcdefolder(n)->list:return[1,2,3,n]As a function,defolder(n)->list:return[1,2,3,n]x=Result.from_func(older,list,n=2)# Important:#  if your function doesn't accept arguments, use "()" at the end to call it properly# Example:x=Result.from_func(no_args)()^Callthefunction