exception

package module
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2022 License: MIT Imports: 4 Imported by: 0

README

Exception

Go test workflow Go Report Card codecov Go Reference

Go Try Catch Exception Handler

By design, Go doesn't offer any mechanism for Exception handling. But Programmers from different backgrounds like Java, C++, Php can be sceptical about the decision. Exception handling with Try Catch Finally is well adapted in all the modern languages. To ease the pain, this library offers utility functions for Exception Handling, which will help programmers to write Go code with Try-Catch-Finally approach.

This how you can throw Exception and handle within Catch:

 import(
     e "github.com/rbrahul/exception"
 )

...
    e.Try(func() {
        data := getValue() // get me the value from Allions
        if data != 100 {
		    e.Throw(e.AssertionError("Expected value is not same as 100"))
        }
	})
    .Catch(e.In(e.AssertionErrorType, e.ValueErrorType), func(excep *Exception) {
        fmt.Println("Message:",excep.Message)
        fmt.Println("Exception Type:",excep.Type)
        fmt.Println("Here is the Stack Trace:",excep.StackTrace)
    })
    .Catch(nil, func(excep *Exception) {
        fmt.Println("I'll be executed as fallback:",excep.Message)
    })
    .Finally(func() {
		fmt.Println("I have been executing always to clean the world!")
	})
    .Run()
...

Throwing a custom exception

You have to define a exception variable with ExceptionType.

const SomethingWentWrongError  e.ExceptionType = "SomethingWentWrongError"

Now you have to initialize and throw your exception via e.New constructor. You can pass a proper error message as optional argument.

    e.Try(func() {
        e.Throw(e.New(SomethingWentWrongError, "Something went worng!"))
	})
    .Catch(e.In(SomethingWentWrongError), func(excep *Exception) {
        fmt.Println("Message:",excep.Message)
        fmt.Println("Exception Type:",excep.Type)
    })
    .Finally(func() {
		fmt.Println("I'm Gonna fix it!")
	})
    .Run()

You can wrap any panic with try-catch and recover it elegently

    e.Try(func() {
        panic("I'm gonna panic but don't worry")
	})
    .Catch(nil, func(excep *Exception) {
        fmt.Println("I knew you are gonna catch me :p", excep.Message)
    })
    .Run()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Throw

func Throw(exp *Exception)

Throw is used to throw an exception. You can use some builtin Exceptions such as LookupError, PermissionError, NetworkError etc. which are offered by this library or you can also create your custom Exception and throw. It accepts error message as an optional argument. Otherwise the ExceptionType is used as default error message.

func Try

func Try(cb func()) *exceptionHandler

Try executes your code and finds if there is any panic or exception and passes the exception to catch block

Types

type Exception

type Exception struct {
	Message    string
	Type       ExceptionType
	StackTrace string
}

func AssertionError

func AssertionError(args ...interface{}) *Exception

AssertionError should be used to throw an exception indicating assertion failure. It accepts error message as an optional argument. Otherwise the ExceptionType is used as default error message.

func ConnectionError

func ConnectionError(args ...interface{}) *Exception

ConnectionError should be used to throw an exception indicating connection related failure. It accepts error message as an optional argument. Otherwise the ExceptionType is used as default error message.

func EOFError

func EOFError(args ...interface{}) *Exception

EOFError should be used to throw an exception indicating end of file related issue. It accepts error message as an optional argument. Otherwise the ExceptionType is used as default error message.

func IndexError

func IndexError(args ...interface{}) *Exception

IndexError should be used to throw an exception indicating index is out of bound failure. It accepts error message as an optional argument. Otherwise the ExceptionType is used as default error message.

func LookupError

func LookupError(args ...interface{}) *Exception

LookupError should be used to throw an exception indicating the key is unavailable in a map. It accepts error message as an optional argument. Otherwise the ExceptionType is used as default error message.

func NetworkError

func NetworkError(args ...interface{}) *Exception

NetworkError should be used to throw an exception indicating the network related issue. It accepts error message as an optional argument. Otherwise the ExceptionType is used as default error message.

func New

func New(exceptionType ExceptionType, args ...interface{}) *Exception

New is constructor which is used to create a new Exception

func PermissionError

func PermissionError(args ...interface{}) *Exception

PermissionError should be used to throw an exception indicating the permission related issue. It accepts error message as an optional argument. Otherwise the ExceptionType is used as default error message.

func ReferenceError

func ReferenceError(args ...interface{}) *Exception

ReferenceError should be used to throw an exception indicating the reference related of issue. It accepts error message as an optional argument. Otherwise the ExceptionType is used as default error message.

func SyntaxError

func SyntaxError(args ...interface{}) *Exception

SyntaxError should be used to throw an exception indicating the reference related of issue. It accepts error message as an optional argument. Otherwise the ExceptionType is used as default error message.

func TimeoutError

func TimeoutError(args ...interface{}) *Exception

TimeoutError should be used to throw an exception indicating the Timeout related issue. It accepts error message as an optional argument. Otherwise the ExceptionType is used as default error message.

func TypeError

func TypeError(args ...interface{}) *Exception

TypeError should be used to throw an exception indicating the type is not as expected. It accepts error message as an optional argument. Otherwise the ExceptionType is used as default error message.

func ValueError

func ValueError(args ...interface{}) *Exception

ValueError should be used to throw an exception indicating the value is not in correct format. It accepts error message as an optional argument. Otherwise the ExceptionType is used as default error message.

type ExceptionType

type ExceptionType string
const (
	UnknownErrorType    ExceptionType = "UnknownError"
	IndexErrorType      ExceptionType = "IndexError"
	RuntimeErrorType    ExceptionType = "RuntimeError"
	ValueErrorType      ExceptionType = "ValueError"
	NetworkErrorType    ExceptionType = "NetworkError"
	SyntaxErrorType     ExceptionType = "SyntaxError"
	PermissionErrorType ExceptionType = "PermissionError"
	TimeoutErrorType    ExceptionType = "TimeoutError"
	TypeErrorType       ExceptionType = "TypeError"
	AssertionErrorType  ExceptionType = "AssertionError"
	ConnectionErrorType ExceptionType = "ConnectionError"
	ReferenceErrorType  ExceptionType = "ReferenceError"
	EOFErrorType        ExceptionType = "EOFError"
	LookupErrorType     ExceptionType = "LookupError"
)

func In

func In(exceptionTypes ...ExceptionType) []ExceptionType

In accepts a variable number of ExceptionType as arguments. It creates a list ExceptionType that will be used for the mathcing of exception.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL