Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printing an error type should print result of .Error() #60

Closed
derekparker opened this issue Feb 5, 2015 · 13 comments
Closed

Printing an error type should print result of .Error() #60

derekparker opened this issue Feb 5, 2015 · 13 comments

Comments

@derekparker
Copy link
Member

Haven't looked into this - but ideally, printing an error type should return the value of its Error method.

@Omie
Copy link
Contributor

Omie commented Aug 28, 2015

I explored code a little for this. If nobody is working on variable printing then I'd like to take this up next.

It will require thinking a bit since there are more of similar type in line [ #61 #116 #117 #122 ]

Have you thought about how to go about this?

Should use a pretty printer package?
http://godoc.org/github.com/kr/pretty
https://github.com/davecgh/go-spew

It can get really messy if we think of all of these together.
package.ExportedSliceOfMap[index][key].MemberStruct.MemberSlice[index] is kind of scary.
Should this be thought of?

@aarzilli
Copy link
Member

I've thought a bit about this and IMHO the optimal way to do this is in three steps

  1. split the code in variables to separate calculation of addresses with reading values, this will help with step 3 and also make implementing a set command possible
  2. change the code that reads values to read them into some data structure instead of producing a string directly, then implement a pretty printer on top of that
  3. once 1 and 2 are in place use go/parser.ParseExpr to parse expressions and write an interpreter for (a subset of) the go expression sublanguage.

Printing the value of an expression like that would then be automatic. AFAIK you won't be able to use a pretty printer because those use reflect that's not going to work across processes.
I have step 1 almost done but I've been holding off publishing it because it depends on my other two PRs.

@derekparker
Copy link
Member Author

@aarzilli you're correct and that sounds like a reasonable plan for the other issues mentioned by @Omie. This issue does not deal with evaluation based off of an expression, it involves adding functionality to call a Go function and store the return value(s) and then essentially print the string that is returned by .Error to the screen.

@kostya-sh
Copy link
Contributor

@derekparker, I think more general functionality to display a variable as fmt.Sprintf("%s", x) or fmt.Sprintf("%v", x) would be quite useful in cases when x is a big.Int or a big.Rat or any other user type with nice String() implementation.

May be a new command can be introduced for this: pf "format spec like %s, %d" s d

Dealing with breakpoints inside String() or Error() methods might be a bit tricky though.

@derekparker
Copy link
Member Author

@kostya-sh are you thinking of trying to use the format string and have Delve eval the variables used and then create the formatted string itself? That's kind of interesting, and could lead to the type of command you've suggested. Either way, we're still going to need to land #119 eventually before 1.0 and it would be nice to just be able to use that to "call" the Error method and read the string that it returns.

@kostya-sh
Copy link
Contributor

@derekparker, no, my example of a new command wasn't very clear. s and d are names of local variables there, they could have been foo and bar. I wasn't aware of #119. With function calls one can just write:

fmt.Sprintf("format spec like %s, %d", foo, bar)

making a new command unnecessary.

@gideondsouza
Copy link

gideondsouza commented Jul 10, 2017

it's been a while here. Is this issue still open? I'm not an expert, I only recently started using go.

I would love to take this up, could someone just give me a few pointers about where to start digging at the source?

@aarzilli
Copy link
Member

@gideondsouza you would have to first implement function calls, the problem there is interacting with the go runtime in a way that isn't fragile and doesn't upset the runtime: figure out how if there is enough stack space, extend the stack if there isn't enough for the call, deal with panics caused by the injected call, figure out how to make the garbage collector happy about a stack frame that would normally be impossible.

@binary132
Copy link

binary132 commented Feb 21, 2018

As a user, I'd really prefer to see the value of the error if I'm step-through debugging / tracing. Maybe this should be some kind of "alternate" print command, like "render" or "format"? (That could also be useful for rendering things like byte buffers, maybe using spew.Dump?)

@derekparker
Copy link
Member Author

@binary132 I'm not sure I completely understand what you mean. The string returned from .Error() should be the formatted value of that error.

@binary132
Copy link

binary132 commented Feb 22, 2018

Yes, print err.Error() should print that string, but print err should show the value of the interface.

If I had my pick, I'd take what @kostya-sh suggested. If forced to choose between print displaying an interface's value or its String() result, I would definitely rather see the real value (plus calling a method could cause side effects, allocations, etc. which print should be expected not to trigger implicitly.)

@derekparker
Copy link
Member Author

@binary132 Ah, I understand what you're saying. Makes sense.

@derekparker
Copy link
Member Author

Going to close this out as technically this is possible now. You can execute call err.Error() and get the string back.

nclifton pushed a commit to nclifton/delve that referenced this issue Feb 24, 2021
* [#176084395] Changed the sms response json

* [#176084395] Checked and fixed up webhook json format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants