Git Product home page Git Product logo

valkyrie's Introduction

Valkyrie

Valkyrie Logo

Build Status

Description

Valkyrie is a utility that helps you aggregate multiple errors in Go, while maintaining thread safety.

Installation

go get -u github.com/gojektech/valkyrie

Usage

Consider the case of an error prone operation, where there is a possibility of encountering multiple, mutually independant errors while running an operation. We can use Valkyrie to collate all the errors into a single error which we can then return:

func errorProneOperation(n int) error {
	// Create a new Multierror instance, which implements the error interface
	errs := new(valkyrie.MultiError)

	for i := 0; i < n; i++ {
		errs.Push(fmt.Sprintf("error in iteration %d", i))
	}
	// When you have to return an error, call the `HasError` method
	// which returns nil if the length of errors is 0, and returns the errs instance itself if its not
	return errs.HasError()
}

Now, the errorProneOperation can be used just like a function that returns a single error:

func main() {
	err := errorProneOperation(3)
	if err != nil {
		fmt.Println("err :", err)
	}
	// Outputs:
	// err : error in iteration 0, error in iteration 1, error in iteration 2
	err = errorProneOperation(0)
	if err != nil {
		fmt.Println("err :", err)
	}
	// Does not output anything
}

For more documentation, you can visit godoc.org.

License

Copyright 2018, GO-JEK Tech (http://gojek.tech)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

valkyrie's People

Contributors

davecheney avatar rshetty avatar sohamkamani avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

valkyrie's Issues

Struct multierror is not support Unwrap method

For correct working go 1.13 error chains (including errors.Is()) needs implementation of interface:

type wrapper interface {
	Unwrap() error
}

For example:
when i use github.com/gojek/heimdall and if at the time of request occurred context cancel, i am check it as if errors.Is(err, context.Canceled) {}, but it's not work because heimdall use valkyrie multierror that does't support method Unwrap.

panic: index out of range

The implements of Error interface will panic when multi-goroutine both Error and Push, because the formattedError defined outside of mutex and formattedError assign through index (index out of range).

I prefer Error inplement like this:

func (m *MultiError) Error() string {
m.mu.Lock()
l := len(m.errs)
m.mu.Unlock()

if l == 0 {
	return ""
}

formattedError := make([]string, l)
for i:=0; i<l;i++ {
	formattedError[i] = m.errs[i].Error()
}

return strings.Join(formattedError, ",")

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.