Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import
- (
- "encoding/json"
- "fmt"
- )
- type DeftyError {
- Error SubError
- }
- type SubError struct {
- Message string
- }
- func(e *DeftyError) Error() string {
- return e.Error.Message
- }
- type Thing struct {
- }
- func(t *Thing) MethodReturningError() error {
- if true {
- return &DeftyError{
- Error: SubError{
- Message: "£400 please",
- }
- }
- } else {
- return fmt.Errorf("Some other type of error")
- }
- }
- func main() {
- mystruct := &Thing{}
- err := mystruct.MethodReturningError()
- if err != nil {
- concreteStruct, ok := err.(*DeftyError)
- if ok {
- // it's our error! json encode it
- output, err := json.Marshal(concreteStruct)
- if err != nil {
- panic(err)
- }
- } else {
- // it's another type of error which we don't know how to marshal/don't want to marshal.
- panic(err)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement