debuggoCritical
How can I get an error message in a string in Go?
Viewed 0 times
errorhowmessagecanstringget
Problem
I referred to this question, A way to get the value of os.Error - string value (Go), but that is not working in my case.
In that
How can I achieve that?
func (t Trans) TicketQty() (intQty int, err string) {
defer func() {
str := recover()
if(str != nil){
err = "an error"
}
}()
Qty := t.TransObject["qty"].(map[string] interface{})["ticket fv"].(float64)
intQty = 10
return
}In that
err, I need the error message that is thrown, e.g., if the parsing logic fails, it is throwing an error automatically. That is the error I need to catch. I am showing this only for example - whatever the runtime exception is, I need to catch it and pass as err in the return.How can I achieve that?
Solution
Use the
The returned error can be treated as a string by either accessing
Don't use
See Error handling and Go, and Defer, Panic and Recover on the Go blog for more info.
Rereading your question, it looks like you're trying to recover from possible failed type assertions. In this instance it's recommended to use the "comma, ok" idiom (mentioned in the previously linked section of the docs), which (slightly paraphrased to be more general) means:
"If the type assertion fails, [the receiver variable] will still exist and be of type [whatever was asserted], but it will have the zero value..."
A simple example to test if an
Will print:
1024.000000 is float64
could not assert that "foo" is float64.
Playground
errors package to create new errors.err = errors.New("an error")The returned error can be treated as a string by either accessing
err.Error(), or using the fmt package functions (for example fmt.Println(err)).Don't use
recover unless you really know what you're doing. It's idiomatic to return all errors, and to deal with them when they arise.See Error handling and Go, and Defer, Panic and Recover on the Go blog for more info.
Rereading your question, it looks like you're trying to recover from possible failed type assertions. In this instance it's recommended to use the "comma, ok" idiom (mentioned in the previously linked section of the docs), which (slightly paraphrased to be more general) means:
"If the type assertion fails, [the receiver variable] will still exist and be of type [whatever was asserted], but it will have the zero value..."
A simple example to test if an
interface{} is actually a float64 through type assertion, and produce a custom error if it fails (instead of panicking):package main
import (
"errors"
"fmt"
)
// assertFloat64 attempts a type assertion to float64.
// It returns a custom error if it fails.
func assertFloat64(n interface{}) error {
// Type assertion. Is n float64?
f, ok := n.(float64)
// If yes,
if ok {
// print the result
fmt.Printf("%f is float64\n", f)
// and return nil error.
return nil
}
// Else return our custom error
return errors.New(fmt.Sprintf("could not assert that \"%v\" is float64.\n", n))
}
func main() {
// Successful
// 1024.0 is float64
err := assertFloat64(1024.0)
if err != nil {
fmt.Println(err)
}
// Failure
// "foo" isn't float64
err = assertFloat64("foo")
if err != nil {
fmt.Println(err)
}
}Will print:
1024.000000 is float64
could not assert that "foo" is float64.
Playground
Code Snippets
err = errors.New("an error")package main
import (
"errors"
"fmt"
)
// assertFloat64 attempts a type assertion to float64.
// It returns a custom error if it fails.
func assertFloat64(n interface{}) error {
// Type assertion. Is n float64?
f, ok := n.(float64)
// If yes,
if ok {
// print the result
fmt.Printf("%f is float64\n", f)
// and return nil error.
return nil
}
// Else return our custom error
return errors.New(fmt.Sprintf("could not assert that \"%v\" is float64.\n", n))
}
func main() {
// Successful
// 1024.0 is float64
err := assertFloat64(1024.0)
if err != nil {
fmt.Println(err)
}
// Failure
// "foo" isn't float64
err = assertFloat64("foo")
if err != nil {
fmt.Println(err)
}
}Context
Stack Overflow Q#22170942, score: 159
Revisions (0)
No revisions yet.