catch.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //Copyright 2017 GoGraphviz Authors
  2. //
  3. //Licensed under the Apache License, Version 2.0 (the "License");
  4. //you may not use this file except in compliance with the License.
  5. //You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. //Unless required by applicable law or agreed to in writing, software
  10. //distributed under the License is distributed on an "AS IS" BASIS,
  11. //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. //See the License for the specific language governing permissions and
  13. //limitations under the License.
  14. package gographviz
  15. import (
  16. "fmt"
  17. "strings"
  18. )
  19. type errInterface interface {
  20. SetStrict(strict bool)
  21. SetDir(directed bool)
  22. SetName(name string)
  23. AddPortEdge(src, srcPort, dst, dstPort string, directed bool, attrs map[string]string)
  24. AddEdge(src, dst string, directed bool, attrs map[string]string)
  25. AddNode(parentGraph string, name string, attrs map[string]string)
  26. AddAttr(parentGraph string, field, value string)
  27. AddSubGraph(parentGraph string, name string, attrs map[string]string)
  28. String() string
  29. getError() error
  30. }
  31. func newErrCatcher(g Interface) errInterface {
  32. return &errCatcher{g, nil}
  33. }
  34. type errCatcher struct {
  35. Interface
  36. errs []error
  37. }
  38. func (e *errCatcher) SetStrict(strict bool) {
  39. if err := e.Interface.SetStrict(strict); err != nil {
  40. e.errs = append(e.errs, err)
  41. }
  42. }
  43. func (e *errCatcher) SetDir(directed bool) {
  44. if err := e.Interface.SetDir(directed); err != nil {
  45. e.errs = append(e.errs, err)
  46. }
  47. }
  48. func (e *errCatcher) SetName(name string) {
  49. if err := e.Interface.SetName(name); err != nil {
  50. e.errs = append(e.errs, err)
  51. }
  52. }
  53. func (e *errCatcher) AddPortEdge(src, srcPort, dst, dstPort string, directed bool, attrs map[string]string) {
  54. if err := e.Interface.AddPortEdge(src, srcPort, dst, dstPort, directed, attrs); err != nil {
  55. e.errs = append(e.errs, err)
  56. }
  57. }
  58. func (e *errCatcher) AddEdge(src, dst string, directed bool, attrs map[string]string) {
  59. if err := e.Interface.AddEdge(src, dst, directed, attrs); err != nil {
  60. e.errs = append(e.errs, err)
  61. }
  62. }
  63. func (e *errCatcher) AddAttr(parentGraph string, field, value string) {
  64. if err := e.Interface.AddAttr(parentGraph, field, value); err != nil {
  65. e.errs = append(e.errs, err)
  66. }
  67. }
  68. func (e *errCatcher) AddSubGraph(parentGraph string, name string, attrs map[string]string) {
  69. if err := e.Interface.AddSubGraph(parentGraph, name, attrs); err != nil {
  70. e.errs = append(e.errs, err)
  71. }
  72. }
  73. func (e *errCatcher) AddNode(parentGraph string, name string, attrs map[string]string) {
  74. if err := e.Interface.AddNode(parentGraph, name, attrs); err != nil {
  75. e.errs = append(e.errs, err)
  76. }
  77. }
  78. func (e *errCatcher) getError() error {
  79. if len(e.errs) == 0 {
  80. return nil
  81. }
  82. ss := make([]string, len(e.errs))
  83. for i, err := range e.errs {
  84. ss[i] = err.Error()
  85. }
  86. return fmt.Errorf("errors: [%s]", strings.Join(ss, ","))
  87. }