All Projects → ez4o → go-try

ez4o / go-try

Licence: MIT license
A package that allows you to use try/catch block in Go.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to go-try

try-to-catch
functional try-catch wrapper for promises
Stars: ✭ 30 (-18.92%)
Mutual labels:  catch, try, try-catch
try-inline
An easy inline error handling wrapper for async promises and syncronous functions
Stars: ✭ 24 (-35.14%)
Mutual labels:  try, try-catch
J-Curry
A Java library that enables applying Functional Programming concepts like currying and partial application for functions, also it supports types like Either, Try, etc... using RxJava 2 interfaces, compatible with Java 7 and above
Stars: ✭ 17 (-54.05%)
Mutual labels:  try
Cmake Examples
Useful CMake Examples
Stars: ✭ 7,220 (+19413.51%)
Mutual labels:  catch
catch cmake coverage
Integration of Catch, CMake and CMake CodeCoverage module and basic examples in C++.
Stars: ✭ 30 (-18.92%)
Mutual labels:  catch
catch-lest-other-comparison
Tabularised feature comparison between Catch, doctest and lest C++ test frameworks
Stars: ✭ 20 (-45.95%)
Mutual labels:  catch
Promise
Asynchronous Programming with Promises
Stars: ✭ 15 (-59.46%)
Mutual labels:  catch
CPP Template
C++ project template : CMake, Test, Travis CI, Appveyor, CodeCoverage, Doxygen
Stars: ✭ 32 (-13.51%)
Mutual labels:  catch
try clojerl
Online Clojerl REPL
Stars: ✭ 26 (-29.73%)
Mutual labels:  try
TYPOTry
A small Vagrant box to try out the most recent TYPO3 release
Stars: ✭ 21 (-43.24%)
Mutual labels:  try
Await To Js
Async await wrapper for easy error handling without try-catch
Stars: ✭ 2,223 (+5908.11%)
Mutual labels:  try-catch
try-catch
Experimental alternative to a try/catch block in JavaScript
Stars: ✭ 31 (-16.22%)
Mutual labels:  try-catch
of
🍬 Promise wrapper with sugar 🍬
Stars: ✭ 13 (-64.86%)
Mutual labels:  try-catch
github-contribution-graph
Add beautiful GitHub contribution/commit graph to your profile README!
Stars: ✭ 37 (+0%)
Mutual labels:  ez4o

Contributors Forks Stargazers MIT License Issues Issues Closed


go-try


Table of Contents
  1. About
  2. Why go-try?
  3. Getting Started
  4. Roadmap
  5. Contributing
  6. License
  7. Contact

About

go-try is a package that allows you to use try/catch block in Go.

This project is still in its early stages, so any thoughts and feedback are very welcome!

(back to top)

Why go-try?

Many Go developers get tired of dealing with errors because there are too many errors to handle one by one, which is intuitive and effective, but really annoying.

I've been trying to find out if Go has an error handling method like try/catch, I think that would probably be a lot easier, but unfortunately, I couldn't find any package that's easy to use.

So I tried to make one myself, taking inspiration from the try/catch syntax, then go-try was born!

(back to top)

Getting Started

Installation

go get github.com/ez4o/go-try

Usage

Try(func () {
  ...
  ThrowOnError(ce)
  ...
  ThrowOnError(err)
  ...
}).Catch(func (ce CustomError) {
  ...
}).Catch(func (e error, st *StackTrace) {
  ...
  st.Print()
})

Functions

Name Description
Try() Takes func (), wrap your code here!
Catch() Takes func (any) or func (any, *StackTrace), and it will only accept the error type you have declared. You can accept second parameter, which is the stack trace begin from the last ThrowOnError().
ThrowOnError() Takes any. Will only throw an error when the parameter is not nil.
st.Print() If you have declared the second parameter st *StackTrace, you can print the stack trace using st.Print().

Example

Let's say you want to fetch JSON from a url and unmarshal it, you can simply write it like this:

import (
  "encoding/json"
  "fmt"
  "io/ioutil"
  "net/http"
    
  . "github.com/ez4o/go-try"
)

func main() {
  Try(func() {
    resp, err := http.Get("https://jsonplaceholder.typicode.com/posts")
    ThrowOnError(err)
    defer resp.Body.Close()
    
    b, err := ioutil.ReadAll(resp.Body)
    ThrowOnError(err)

    var data []map[string]interface{}
    err = json.Unmarshal(b, &data)
    ThrowOnError(err)

    fmt.Println(data)
  }).Catch(func(e error, st *StackTrace) {
    fmt.Println(e)
    st.Print()
  })
}

For more examples, head over to https://github.com/ez4o/go-try/tree/main/.examples!

(back to top)

Roadmap

  • Implement catching errors of different types.
  • Tests
  • CI

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feat/amazing-feature)
  3. Commit your Changes with Conventional Commits
  4. Push to the Branch (git push origin feat/amazing-feature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Contact

Author

Project Link

(back to top)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].