Code Monkey home page Code Monkey logo

merry's People

Contributors

ansel1 avatar dependabot[bot] avatar emanuelsteen avatar stroborobo 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  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

merry's Issues

Add flag with causes e.Error() to return the same as merry.Details(e)

Sometimes, errors are passed from merry-aware code to merry-unaware code. If it's the unaware code which is responsible for printing the error, it won't print the details. Might be helpful to have a flag on the merry error which changes the behavior of Error() to return the same as merry.Details(e).

This came up in unit tests. I use an assertions library (testify) which checks for errors. If there's an error, it prints it. I end up having to wrap the assertion library in order to print out the full merry details of the error.

Add a Cause property

Add a new native merry error property: "cause". This would be another error. Useful when a library emits errors of fixed types, and wants to wrap errors from lower level libraries in its own error types without losing information.

Details() would be enhanced to print the stack of causes, perhaps labelling the innermost cause the "root cause".

Cause(err) would get the next error down the cause stack. RootCause(err) would get the deepest cause.

Could also consider enhancing Is(), or adding an alternate form, for comparing against any of the causes in the stack.

merry v1.6.0 fails go get

$ go get github.com/ansel1/merry
go get: github.com/ansel1/[email protected] updating to
        github.com/ansel1/[email protected] requires
        github.com/ansel1/merry/[email protected]: invalid version: unknown revision 000000000000

I also note that there is no v2 release tag yet (only beta). I'm guessing making a v2.0.0 release tag and updating the v1 go.mod to point to that release tag would fix it.

I'm not sure if replace tags work as expected when a module is being imported as a library.

Support a way to set both the user message and the internal message to the same thing when creating a new error

Example: in a web app's controller, you want to raise an error with a user message. The internal error message should be same. Today, you need to make two calls with the same message argument to achieve this. Would be nice to express "use this message for the internal message and the user message".

There are a few approaches:

  1. A set of methods which set both the internal and user message
  2. A function which fetches the user message first, and falls back on the internal message if no user message is set
  3. A function which fetches the internal error, and falls back on the user message if not set

I'm leaning towards three. Two seems wrong because the whole purpose of the "user message" property is for frameworks to ensure that error messages presented to end users have been explicitly approved for end user consumption.

Should Location() return empty string?

Hi,

I feel like Location() should return an empty string if it's unknown where the error happened.

  • It's the null value of strings
  • A file with that name could actually exist (although unlikely)
  • Naming a file "" is probably impossible everywhere
  • Other functions like Stacktrace() return an empty string as well, not "no stacktrace" :)

What do you think?

Tests fail (nil value handling)

In 99536c2 you changed the nil value handling and added a test for this (TestNilValues). It contains the following line:

if Wrap(nil).WithHTTPCode(400).WithMessage("hey") { //...

Maybe I misunderstand your commit, but you can't call methods on "true" nil interfaces, can you?

http://play.golang.org/p/mmJoXJR9J1

Did you mean

if (WithMessage(WithHTTPCode(Wrap(nil), 400), "hey") != nil) { //...

(This will still crash since WithValue doesn't handle the nil from the Wrap call it does.)

Support callbacks to add context to a merry error when it is first captured

When a merry error is first created, or first wrapped around an existing error, it would be cool to optionally invoke configurable a callback. This would allow users of merry to:

  1. capture a different kind of stacktrace (e.g. sentry stacktraces)
  2. attach other context information to the error.
  3. send the error to a log or error capture system

Callbacks would be registered globally. They would be invoked whenever a new merry error is created, or when an error was wrapped with a merry error. They would be passed the current merry error, and a stackframe offset from where the merry error is being created, and would return a merry error.

merry.Errorf returns a stdlib error in some cases which breaks %+v

Actual: fmt.Printf("%+v", merry.Errorf("%w", merry.New("boom"))) does not print a stacktrace
Expected: It should print a stacktrace. Maybe the stacktrace of the wrapped error.

This example reproduces the issue - https://go.dev/play/p/15d5C3TAbDh .

As far as I understand the code, merry.Wrap called by merry.Errorf does not wrap the argument if it finds an existing stack trace in its wrap chain. Since merry.New("boom") is in the chain, merry.Errorf just returns the result of fmt.Errorf . However, in that case, fmt.Printf("%+v") will not use the Formatter interface implementation in merry.

Workaround: add any value. merry.Errorf("%w", merry.New("boom"), merry.WithValue("foo", "bar")) works fine.

Add a way to use predefined merry errors to wrap

We generally define constants for often-used errors:

var (
  ErrInvalidParameter = merry.
    New("Invalid parameter").
    WithUserMessage("Invalid parameter").
    WithHTTPCode(http.StatusBadRequest)
)

Then in the controller I'd love to be able to write something like:

result, err := strconv.ParseBool(param)
if err != nil {
  // this should save the current stack and the err object, but maybe override the http code
  // and user/error message if present.
  return ErrInvalidParameter.Wrap(err)
}

merry.Details() does not print attached values

Calling merry.Details(err) does not print an error's attached values, i.e. merry.New("x").WithValue("lololol", "asdasdasd").

Could it be made to print it? If it is just a matter of implementation, I would be happy to submit a pull request.

Value extraction

Hi! Could you please add a method extracting all the values from an error? Like map[string]interface{} or some iterator over them.

Deal with nil errors

Most functions which take errors should allow for nil to be passed, and should return nil in these cases.

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.