m3lsh

package module
v0.0.0-...-26ef5d8 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2018 License: LGPL-3.0 Imports: 5 Imported by: 0

README

M3lsh

Codacy Badge Build Status Codacy Badge

A library for exception handling in Golang, it simply uses panic with the ability to Try/TryCatch exceptions and Throw custom exceptions with details of stack trace and thread info. You can use multiple catch "statements", only the first matching statement will be executed. Due to golang's limitations, exceptions will be sent as interface{} to your method, but they will always be castable to the type you specify. Catching a &m3lsh.BaseException{} will catch any exception.

Installation

go get github.com/mohamed-essam/m3lsh

Or using dep, add to your Gopkg.toml

[[constraint]]
	name = "github.com/mohamed-essam/m3lsh"
	revision = "<latest master revision>"

Usage

import (
	"m3lsh"
	"fmt"
)

type MyException struct {
	m3lsh.BaseException
}

func main() {
	m3lsh.TryCatch(func() {
		m3lsh.Throw(&MyException{}, "Cats")
	}, m3lsh.Catcher(&MyException{}, func(e interface{}){
		fmt.Printf("%T", ex) // *MyException
		ex := e.(*MyException)
		fmt.Printf(ex.Message) // Cats
	}), m3lsh.Catcher(&m3lsh.BaseException{}, func(e interface{}){
		// This will catch all exceptions other than MyException
	})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Throw

func Throw(ex exception, message string)

Throw Format and throw error message with type and message

func Try

func Try(function tryFn) (err exception)

Try catch all errors and exceptions and return them without failing

func TryCatch

func TryCatch(function tryFn, catchers ...*CatcherWrapper) (err exception)

TryCatch catch errors specified in catchers, if an *m3lsh.BaseException is given it takes any kind of exception, otherwise, exception Type must exactly match given type

Types

type BaseException

type BaseException struct {
	Message string
	// contains filtered or unexported fields
}

BaseException parent of all exceptions, implements some basic methods and acts as a catch-all exception type

func (BaseException) Stacktrace

func (b BaseException) Stacktrace() []string

Stacktrace get printable stack trace lines

type CatcherWrapper

type CatcherWrapper struct {
	// contains filtered or unexported fields
}

CatcherWrapper a wrapper for an exception catch function

func Catcher

func Catcher(typ exception, fn catchFn) *CatcherWrapper

Catcher generate CatcherWrapper for a specific exception type with a handler

Jump to

Keyboard shortcuts

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