Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. package main
  2.  
  3. import
  4. (
  5. "encoding/json"
  6. "fmt"
  7. )
  8.  
  9. type DeftyError {
  10. Error SubError
  11. }
  12.  
  13. type SubError struct {
  14. Message string
  15. }
  16.  
  17. func(e *DeftyError) Error() string {
  18. return e.Error.Message
  19. }
  20.  
  21. type Thing struct {
  22. }
  23.  
  24. func(t *Thing) MethodReturningError() error {
  25. if true {
  26. return &DeftyError{
  27. Error: SubError{
  28. Message: "£400 please",
  29. }
  30. }
  31. } else {
  32. return fmt.Errorf("Some other type of error")
  33. }
  34. }
  35. func main() {
  36. mystruct := &Thing{}
  37. err := mystruct.MethodReturningError()
  38. if err != nil {
  39. concreteStruct, ok := err.(*DeftyError)
  40. if ok {
  41. // it's our error! json encode it
  42. output, err := json.Marshal(concreteStruct)
  43. if err != nil {
  44. panic(err)
  45. }
  46. } else {
  47. // it's another type of error which we don't know how to marshal/don't want to marshal.
  48. panic(err)
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement