Giter VIP home page Giter VIP logo

Comments (5)

odeke-em avatar odeke-em commented on May 16, 2024

At @broady you mean this

// Handler for the incoming requests.
func demoHandler(w http.ResponseWriter, r *http.Request) {
// How to handle a panic.
defer func() {
if e := recover(); e != nil {
stack := make([]byte, 1<<16)
stackSize := runtime.Stack(stack, true)
report(string(stack[:stackSize]), r)
}
}()
// Panic is triggered.
x := 0
log.Println(100500 / x)
}

IMHO it suffices and doesn't need removing to use panic, because it shows the user how to capture the stacktrace for their bug report. What do you think?

from golang-samples.

broady avatar broady commented on May 16, 2024

Yep, that one.

I guess it's fine, but I don't fully understand the usage of error reporting for Go. Do we really want people to only use it for panics?

/cc @rakyll

from golang-samples.

 avatar commented on May 16, 2024

There's a package that may simplify the code a little; github.com/pkg/errors
Thus

import (
    "github.com/pkg/errors"
)
... 
// Handler for the incoming requests. 
func demoHandler(w http.ResponseWriter, r *http.Request) { 
 	err := errors.Error("Runtime Issue with demoHandler")
        if err != nil {
            report(err, r)
        }
}
func report(err Error, r *http.Request) {
        ...
        "stacktrace": errors.Errorf("%+v", err)
        ...
}

I'm not sure, though, how helpful stack traces are as Rob Pike has written that errors should be given context before before bubbled up the stack or dealt with without causing the system to fall over. Using the errors pkg we could now wrap one error context within another potentially creating a much nicer and helpful error message rather than a stack trace (remembers screens full of java stack traces....!) Just my two pennies.

from golang-samples.

tbpg avatar tbpg commented on May 16, 2024

@broady @odeke-em I haven't seen any feedback about this. Do you still have ideas about how to improve it, or should we close this?

@Brotherhod Thanks for the link and example. I'd prefer to avoid using any additional external dependencies.

from golang-samples.

tbpg avatar tbpg commented on May 16, 2024

Declaring issue bankruptcy on this. Feel free to reopen if you want to fix it.

from golang-samples.

Related Issues (20)

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.