• Stars
    star
    29
  • Rank 853,791 (Top 17 %)
  • Language
    Go
  • License
    MIT License
  • Created over 2 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple utility package for exception handling with try-catch in Golang

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 is 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()

More Repositories

1

Awesome-JSON-Viewer

πŸ”₯ A Chrome extension to visualise JSON response and introduce awesome JSON prettifying experiences.
JavaScript
535
star
2

gofp

A super simple Lodash like utility library with essential functions that empowers the development in Go
Go
143
star
3

deno_cron

A cron Job scheduler for Deno that allows you to write human readable cron syntax with tons of flexibility
TypeScript
100
star
4

Smart-Webpage-Ruler

A Smart Page Ruler to measure the shape, position and alignment of elements in webpage
JavaScript
31
star
5

desktop-app-using-electron-react

A user friendly Task Manager Cross OS Desktop Application Built With Electron and Reactjs
JavaScript
27
star
6

number-to-bengali-word

An amazing package to convert your number to bengali word representation.
JavaScript
26
star
7

Twitter-bot

Twitter bot which waits for #hashTag and sends weather update of city via tweets
HTML
19
star
8

retry

An essential retry-operation related library for Golang to build fault-tolerant system.
Go
14
star
9

unwanted-email-cleaner

Clean Unwanted emails from your Gmail account filtered by keywords
JavaScript
13
star
10

Slack-bot

Slack Schedule Messaging BOT
JavaScript
13
star
11

SimpleWebEditor

A simple html,css and javascript editor to write and test html,css and javascript in realtime.
JavaScript
8
star
12

large-file-finder

This simple CLI application helps you to find giant files that are eating up your system storage
Python
7
star
13

canvas-editor

This project has been developed based on node.js and pure javascript with out any third party front end library
JavaScript
7
star
14

redux-re-dispatch

A redux middleware to dispatch recent actions from action history Queue
JavaScript
4
star
15

voice-to-shell-commands

An application to execute command from your voice.
JavaScript
3
star
16

amazon-price-alert-extension

Amazon Price Alert Chrome Extension
JavaScript
2
star
17

larablog

This is a simple blog and Image Gallery for CRUD operation, built with Laravel 5.1 PHP Framework
PHP
1
star
18

word-matching-game-for-kids

Small Word Matching Game for Kids built with ReactJS
JavaScript
1
star
19

scrapper-using-nodejs

JavaScript
1
star