Skip to content

Raising custom error types/ const errors #160

Open

Description

@andig

The io package declares an EOF error like this:

var EOF = errors.New("EOF")

using this pattern to raise custom errors leads to ugly stack traces since the errors are generated during intialization, not when they happen:

github.com/andig/gosml.init
        /Users/andig/Documents/htdocs/go/src/github.com/andig/gosml/crc16.go:32
main.init
        <autogenerated>:1
runtime.main
        /usr/local/opt/go/libexec/src/runtime/proc.go:186
runtime.goexit
        /usr/local/opt/go/libexec/src/runtime/asm_amd64.s:2361

in fact, the entire call stack of where is error happens is lost which means using the error type is impossible.

How could one implement custom error types or errors the can be behaviour-checked according to your blog article?

Activity

  1. CreatCodeBuild commented on Jun 18, 2018

    @CreatCodeBuild

    you can always wrap it again

  2. puellanivis commented on Jun 18, 2018

    @puellanivis
  3. davecheney commented on Jun 18, 2018

    @davecheney
    Member
  4. andig commented on Jun 19, 2018

    @andig
    Author

    The errors package explicitly makes it hard to use for Singleton errors.

    Understood and thank you for your blog posts.

    I suggest using the stdlib errors.new and wrapping when needed.

    That works but still doesn't deliver and entirely "clean" stack trace as I'd desire.

    Bottom line- with my limited go experience- seems to be that you cannot test wrapped (top-level) errors for behaviour (need to use causer instead) and stack traces will contain both inner error creation plus wrapping when "throwing" for that reason.

  5. davecheney commented on Jun 19, 2018

    @davecheney
    Member
  6. puellanivis commented on Jun 19, 2018

    @puellanivis

    Indeed, I recall in a previous bug/feature discussion, I suggested making a nicer Sentinel const-compatible type, and the recommendation was, “if we make it easier, people will use it, and we would like to actually discourage it.“

    But regardless, a “good sentinel” value can be had with just a few lines of code. Sure the type ends up being unique to your package, (and hopefully actually unexported) but these are parts of what should hopefully make sentinel errors better than var EOF = errors.New("EOF") with all the pitfalls of being able to then monkey around with the sentinel.

    As a note, the stdlib errors is not const-compatible, so. 😐

  7. davecheney commented on Oct 8, 2018

    @davecheney
    Member

    I'm sorry I haven't replied before now.
    I'm going to close this issue as there is nothing to do here; this package isn't what you what, its designed in a way that is incompatible with your requirements.

  8. added 2 commits that reference this issue on Jan 5, 2019
    d71697b
    4f47277
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions